Problem with template

D

desktop

When I try to compile this template I get an error in the line:

"enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};"

saying:

blop.hpp:8: error: expected primary-expression before ‘>’ token


#ifndef BLOP_HPP_
#define BLOP_HPP_


template<typename T>
class IsClassT {
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};
enum { No = !Yes};
//int getInt(){return a;};

private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test();
static const int a = 10;


};

Any ideas why I get this error?
 
S

Sylvester Hesp

desktop said:
When I try to compile this template I get an error in the line:

"enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};"

saying:

blop.hpp:8: error: expected primary-expression before ‘>’ token


#ifndef BLOP_HPP_
#define BLOP_HPP_


template<typename T>
class IsClassT {
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};
enum { No = !Yes};
//int getInt(){return a;};

private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test();
static const int a = 10;


};

Any ideas why I get this error?

IsClassT<T>::test is a dependent name, so you'll have to give the compiler a
hint so that it knows it's a template (instead of just testing whether
IsClassT<T>::test is smaller than T, which is an error as T is a type, not
an expression). Try this:

enum { Yes = sizeof(IsClassT<T>::template test<T>(0)) == 1};

- Sylvester Hesp
 
A

Alf P. Steinbach

* desktop:
When I try to compile this template I get an error in the line:

"enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};"

saying:

blop.hpp:8: error: expected primary-expression before ‘>’ token


#ifndef BLOP_HPP_
#define BLOP_HPP_


template<typename T>
class IsClassT {
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};
enum { No = !Yes};
//int getInt(){return a;};

private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test();
static const int a = 10;


};

Any ideas why I get this error?

One reason is that it's not known at that point that "test" is a
template. You could fix that by inserting the word "template", but
there are other problems. Try

template< typename T >
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(...);
public:
enum { yes = sizeof(test<T>(0)) == 1};
enum { no = !yes};
};
 
D

desktop

Alf said:
* desktop:
When I try to compile this template I get an error in the line:

"enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};"

saying:

blop.hpp:8: error: expected primary-expression before ‘>’ token


#ifndef BLOP_HPP_
#define BLOP_HPP_


template<typename T>
class IsClassT {
public:
enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};
enum { No = !Yes};
//int getInt(){return a;};
private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test();
static const int a = 10;
};

Any ideas why I get this error?

One reason is that it's not known at that point that "test" is a
template. You could fix that by inserting the word "template", but
there are other problems. Try

template< typename T >
class IsClassT
{
private:
typedef char One;
typedef struct { char a[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(...);
public:
enum { yes = sizeof(test<T>(0)) == 1};
enum { no = !yes};
};

You prefer to change:

enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};

to:

enum { yes = sizeof(test<T>(0)) == 1};

but what if 'test' was a previous declared function outside IsClassT,
how would the compiler know which one to choose?

Besides from that I don't understand why it is illegal to access a
member in a class like:

IsClassT<T>::test<T>(0)

since that is how it is normally done (by using the qualify operator)
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top