Template traits

A

Aarti

I was trying my hand on a template class and i am getting a
compilation error

error C2146: syntax error : missing ';' before identifier
'isOptimizedImplAvailable'. Any pointers would be of great help.
Here is my code

#include <iostream>

using namespace std;

template<typename T>
class foo_traits
{
public:
static bool customImpl;
};

template<>
class foo_traits<int>
{
public:
static bool customImpl;
};

template<>
class foo_traits<char>
{
public:
static bool customImpl;
};

bool foo_traits<char>::customImpl = false;
bool foo_traits<int>::customImpl = true;

template<typename T>
class foo
{
public:
typename foo_traits<T>::customImpl isOptimizedImplAvailable()
{
return foo_traits<T>::customImpl;
}
};



int main()
{
foo<int> f;
}
 
K

Kai-Uwe Bux

Aarti said:
I was trying my hand on a template class and i am getting a
compilation error

error C2146: syntax error : missing ';' before identifier
'isOptimizedImplAvailable'. Any pointers would be of great help.
Here is my code

#include <iostream>

using namespace std;

template<typename T>
class foo_traits
{
public:
static bool customImpl;
};

template<>
class foo_traits<int>
{
public:
static bool customImpl;
};

template<>
class foo_traits<char>
{
public:
static bool customImpl;
};

bool foo_traits<char>::customImpl = false;
bool foo_traits<int>::customImpl = true;

template<typename T>
class foo
{
public:
typename foo_traits<T>::customImpl isOptimizedImplAvailable()

bool isOptimizedImplAvailable()
{
return foo_traits<T>::customImpl;
}
};



int main()
{
foo<int> f;
}


Best

Kai-Uwe Bux
 
A

Aarti

bool isOptimizedImplAvailable()



Best

Kai-Uwe Bux- Hide quoted text -

- Show quoted text -

WOW that worked. But just for curiosity.. why did returning
foo_traits<T>::customImpl fail?
 
K

Kai-Uwe Bux

Aarti said:
WOW that worked. But just for curiosity.. why did returning
foo_traits<T>::customImpl fail?

Because foo_traits<T>::customImpl is not a type but a boolean static
variable. Putting the keyword "typename" before that identifier will not
magically change its meaning.


Best

Kai-Uwe Bux
Best
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top