template question

  • Follow


Here is the code:

template<typename Target, typename Source>
void functionTemplate(Source arg){}

template<typename Target, typename Source>
class ClassTemplate
{
public: ClassTemplate(Source s){}
};

int main(int argc, char* argv[])
{
int i = 5;
functionTemplate<unsigned char>(i);

ClassTemplate<unsigned char, int> u(i);

// ClassTemplate<unsigned char> u(i); // why does this not work.

return 0;
}

For the function template the compiler deduces the parameters from the
function class, but it does not do that for the class template.Why?


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply michael782 (11) 11/28/2003 11:41:23 PM

Michael D. Borghardt <michael@borghardtConsulting.net> wrote:

 > Here is the code:
 >
 > template<typename Target, typename Source>
 > void functionTemplate(Source arg){}
 >
 > template<typename Target, typename Source>
 > class ClassTemplate
 > {
 > public:
 >  //  ClassTemplate(Source s){}
//  This will take any argument type to get
//  reasonable error messages for really incompatable
//  types requires some template
//  'magic' beyond a message length is needed.
    template <class U>
    ClassTemplate(U u) {}

 > };
 >
 > int main(int argc, char* argv[])
 > {
 > int i = 5;
 > functionTemplate<unsigned char>(i);
 >
 > ClassTemplate<unsigned char, int> u(i);
 >
 > // ClassTemplate<unsigned char> u(i); // why does this not work.
 >
 > return 0;
 > }
 >
 > For the function template the compiler deduces the parameters from the
 > function class, but it does not do that for the class template.Why?
 >
  if you have a templated constructor it will do the same thing, but
if the arguments are not convertable then you may get 'miles of error
messages'in a real program. [see changes above]

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply cbarron3 11/29/2003 10:55:18 AM


Thanks Carl. I am going to be reading more about them in C++ Templates

Here is what I am trying to do. I am trying to ensure any conversions if
fundamental types does not lose any range. The original concept came from an
article I read from Scott Meyers about using unsigned in interfaces. It has
evolve in my mind to the code below.

#include <iostream>
#include <boost/cast.hpp>

template<typename Target>
class numeric_class
{
public:
     template<typename Source>numeric_class(Source s){value_ =
boost::numeric_cast<Target>(s);};
     operator Target () {return value_;};
private:
     Target value_;
};

class Foo{};

void Bar(numeric_class<unsigned long> l)
{
     std::cout << "at this point we know parameter is valid" << endl;
}

int main()
{
// This will not compile
//    Foo f;
//    Bar(f);

// This will throw an exception
     try
     {
         long l = -500;
         Bar(l);
     }
     catch (boost::bad_numeric_cast)
     {
     }

// This will be OK
     try
     {
         float f = 500.0;
         Bar(f);
     }
     catch (boost::bad_numeric_cast)
     {
     }
     return 0;
}


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Michael 11/30/2003 10:25:42 AM

2 Replies
139 Views

(page loaded in 0.089 seconds)

Similiar Articles:













7/14/2012 6:06:47 AM


Reply: