Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template traits

Reply
Thread Tools

Template traits

 
 
Aarti
Guest
Posts: n/a
 
      07-13-2007
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;
}

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-13-2007
Aarti wrote:

> 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

 
Reply With Quote
 
 
 
 
Aarti
Guest
Posts: n/a
 
      07-13-2007
On Jul 12, 9:02 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Aarti wrote:
> > 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- Hide quoted text -
>
> - Show quoted text -


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

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      07-13-2007
Aarti wrote:

> On Jul 12, 9:02 pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> Aarti wrote:
>> > 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- Hide quoted text -
>>
>> - Show quoted text -

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type traits with non-template variadic function Qi C++ 1 10-25-2012 08:39 AM
How to generate execute file that include enthought.traits.api ,enthought.traits.ui.api ? ray Python 1 06-04-2010 03:49 PM
question on template, traits class and static inline Hong Ye C++ 5 06-02-2007 04:07 AM
Is it possible to use type traits as a template parameter? Raider C++ 3 05-12-2006 12:48 PM
templates - container traits skscpp C++ 0 07-03-2003 02:59 PM



Advertisments