templates in parameters

S

ssylee

Post it here.  Don't make people go to some unknown web site,
potentially full of who knows what...




V

Sorry for the inconvenience. I was trying to make it easier to read
(as I don't know how to use code tags here [if i can use code tags
here]).

template <class T>
// MyType is a struct
MyType ConvFunc(T input)
{
// conversion manipulation that has nothing to do with the
conversion
}
 
M

mlimber

I'm trying to template a function as follows in the pastebin
link(http://pastebin.com/m5e642c72).
Would I call the function like as follows:

MyType p = ConvFunc <RECT> (rectangle);

when rectangle is of type RECT?

The code is:

struct MyType { /*...*/ };

template <class T>
MyType ConvFunc(T input)
{
// ...
}

You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:

template<class Ret, class Arg>
Ret Conv( const Arg& arg )
{
// ...
}

void Foo( const int n )
{
MyType m1 = Conv( n ); // Error! Can't figure out return type
MyType m2 = Conv<MyType>( n ); // Ok
MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
// ...
}

Cheers! --M
 
S

ssylee

The code is:

 struct MyType { /*...*/ };

 template <class T>
 MyType ConvFunc(T input)
 {
    // ...
 }

You don't need to specify the type of a parameter (unless you perhaps
wanted a derived class to be converted to its base class or something)
since the compiler can deduce it automatically. If the return value
were customizable, then you would need to specify that since it is not
a function argument and therefore not deducible:

 template<class Ret, class Arg>
 Ret Conv( const Arg& arg )
 {
   // ...
 }

 void Foo( const int n )
 {
   MyType m1 = Conv( n );  // Error! Can't figure out return type
   MyType m2 = Conv<MyType>( n );     // Ok
   MyType m3 = Conv<MyType,int>( n ); // Ok but redundant
   MyType m4 = Conv<MyType,double>( n ); // Ok, forces conversion
   // ...
 }

Cheers! --M

Thanks. I think you misunderstood what I'm trying to do. I'm trying to
convert the parameter (which could be in many different types) into
MyType. Would I need to use the power of templates to achieve the goal?
 
S

ssylee

Sorry for the inconvenience. I was trying to make it easier to read
(as I don't know how to use code tags here [if i can use code tags
here]).
template <class T>
// MyType is a struct
MyType ConvFunc(T input)
{
       // conversion manipulation that has nothing to do with the
conversion
}

I am not sure what code tags are, sorry about making it inconvenient to
you.  We post code here.  Code is code, the alphabet is limited, the
numbers are common and the special symbols are in most cases don't need
to be tri- or di-graphed.  Read the group and get the feel before
posting, it's never too late to learn the rules.

As for the function

     template<class T> void foo(T t);

, you don't need to supply the template argument if your function
argument is already of that type, the compiler will take care of the
template argument deduction.  If you do want to call a *different*
function with a particular argument, do supply it.  If both are the
same, it won't matter.  For, example, if I want to call my 'foo'
instantiated with 'unsigned' as its template argument, but supply an
integer, I would do

     foo<unsigned>(42);

, for example.  Since 42 is already an int, I don't have to write

     foo<int>(42);

, IOW, it's the same as writing

     foo(42); // compiler deduces T == int

V

Thanks for the clarification Victor. I thought the parameters are
different types, but after taking a closer look, they are the same
type.
 
M

mlimber

Thanks. I think you misunderstood what I'm trying to do. I'm trying to
convert the parameter (which could be in many different types) into
MyType. Would I need to use the power of templates to achieve the goal?

I don't believe I misunderstood you. I answered your question in the
sentence after the first code block and then attempted to demonstrate
a more general case where a function Conv() could convert between any
objects. Note also that you could use an overloaded cast operator to
do a conversion if the result type is a class:

#include <sstream>

class A
{
int i_;
public:
// ...

// Convert an A to a string automatically
operator std::string() const
{
std::eek:stringstream oss;
oss << i_;
}
};

void Foo( const A& a )
{
// These all do the same thing
std::string s1 = a; // implicitly calls A::eek:perator std::string()
std::string s2 = static_cast<std::string>( a ); // Explicit
std::string s3 = Conv<std::string>( a ); // Use the func above
// ...
}

Cheers! --M
 
M

mlimber

I don't believe I misunderstood you. I answered your question in the
sentence after the first code block and then attempted to demonstrate
a more general case where a function Conv() could convert between any
objects. Note also that you could use an overloaded cast operator to
do a conversion if the result type is a class:

 #include <sstream>

 class A
 {
   int i_;
 public:
   // ...

   // Convert an A to a string automatically
   operator std::string() const
   {
     std::eek:stringstream oss;
     oss << i_;

Forgot:

return oss.str();
   }
 };

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,927
Members
47,469
Latest member
benny001

Latest Threads

Top