struct Test
{
template <class T>
void print(T& t)
{
t.print();
}
};
struct A
{
void print() {printf( "A");}
};
struct B
{
void print() {printf( "B");}
};
void test_it()
{
A a;
B b;
Test t;
t.print(a);
t.print(b);
}
This compiles fine.
struct Test
{
template <class T>
void print(T& t)
{
t.print();
}
};
void test_it()
{
struct A
{
void print() {printf( "A");}
};
struct B
{
void print() {printf( "B");}
};
A a;
B b;
Test t;
t.print(a);
t.print(b);
}
This fails with error : no matching function for call to 'Test::print(test_it()::A&)'
Can anyone explain me why this happen ? Thanks!!!
Template argument deduction is used when selecting user-defined conversion function template arguments. A is the type that is required as the result of the conversion. P is the return type of the conversion function template.
Member functions can be function templates in several contexts. All functions of class templates are generic but are not referred to as member templates or member function templates. If these member functions take their own template arguments, they are considered to be member function templates.
To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.
Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.
In your second example, A
and B
are local types, which can't be used as template type arguments in C++03 as per §14.3.1/2:
A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With