V
Vladimir Jovic
Hello,
I tried to compile an example from the "c++ templates - the complete
guide", and got an error.
Can anyone explain why error appeared? And how to fix it?
Here is the example :
// CODE START
#include <iostream>
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template< typename U > static One test(...);
template< typename U > static Two test(U (*)[1]);
public:
enum { Yes = ( sizeof( IsFunctionT< T >::test< T >(NULL) ) == 1 ) };
enum { No = !Yes };
};
void hehe(int , double , const char*[])
{
}
class A
{
};
int main()
{
std::cout << "void hehe(int , double , const char*[]) is a function : "
<< IsFunctionT< &hehe >::Yes << std::endl;
std::cout << "class A is a function : "
<< IsFunctionT< A >::Yes << std::endl;
}
// CODE END
The error is :
ops.cpp: In function ‘int main()’:
ops.cpp:27: error: type/value mismatch at argument 1 in template
parameter list for ‘template<class T> class IsFunctionT’
ops.cpp:27: error: expected a type, got ‘hehe’
ops.cpp: At global scope:
ops.cpp: In instantiation of ‘IsFunctionT<A>’:
ops.cpp:29: instantiated from here
ops.cpp:12: error: invalid application of ‘sizeof’ to incomplete type
‘IsFunctionT<A>’
I tried this :
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template< typename U > static One test(...);
template< typename U > static Two test(U (*)[1]);
public:
enum { Yes = ( sizeof( IsFunctionT< T > ) == 1 ) }; <<<<<<<<<
enum { No = !Yes };
};
and even this fails with next error message:
ops.cpp:31: instantiated from here
ops.cpp:12: error: invalid application of ‘sizeof’ to incomplete type
‘IsFunctionT<A>’
I tried to compile an example from the "c++ templates - the complete
guide", and got an error.
Can anyone explain why error appeared? And how to fix it?
Here is the example :
// CODE START
#include <iostream>
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template< typename U > static One test(...);
template< typename U > static Two test(U (*)[1]);
public:
enum { Yes = ( sizeof( IsFunctionT< T >::test< T >(NULL) ) == 1 ) };
enum { No = !Yes };
};
void hehe(int , double , const char*[])
{
}
class A
{
};
int main()
{
std::cout << "void hehe(int , double , const char*[]) is a function : "
<< IsFunctionT< &hehe >::Yes << std::endl;
std::cout << "class A is a function : "
<< IsFunctionT< A >::Yes << std::endl;
}
// CODE END
The error is :
ops.cpp: In function ‘int main()’:
ops.cpp:27: error: type/value mismatch at argument 1 in template
parameter list for ‘template<class T> class IsFunctionT’
ops.cpp:27: error: expected a type, got ‘hehe’
ops.cpp: At global scope:
ops.cpp: In instantiation of ‘IsFunctionT<A>’:
ops.cpp:29: instantiated from here
ops.cpp:12: error: invalid application of ‘sizeof’ to incomplete type
‘IsFunctionT<A>’
I tried this :
template<typename T>
class IsFunctionT {
private:
typedef char One;
typedef struct { char a[2]; } Two;
template< typename U > static One test(...);
template< typename U > static Two test(U (*)[1]);
public:
enum { Yes = ( sizeof( IsFunctionT< T > ) == 1 ) }; <<<<<<<<<
enum { No = !Yes };
};
and even this fails with next error message:
ops.cpp:31: instantiated from here
ops.cpp:12: error: invalid application of ‘sizeof’ to incomplete type
‘IsFunctionT<A>’