473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Template traits

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<intf;
}

Jul 13 '07 #1
3 1372
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<intf;
}

Best

Kai-Uwe Bux

Jul 13 '07 #2
On Jul 12, 9:02 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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<intf;
}

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?

Jul 13 '07 #3
Aarti wrote:
On Jul 12, 9:02 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>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<intf;
}

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
Jul 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marc Schellens | last post by:
I have a template like: template< typename B> class A: public B { typedef typename B::Ty Ty; A( Ty&); A( string); };
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
1
by: paidojoao-groups | last post by:
Given this code: #include <iostream> #include <string> class timestamp { public: timestamp() {
4
by: Martin MacRobert | last post by:
Hi Gang, The following code does not compile, but I can't figure out why. The compiler complains that the CuriouslyDerivedType (CRDerived) does not publish the "value_type", yet in fact the class...
13
by: Imre | last post by:
Please take a look at the following code: template <typename T, typename Enable = void> class A { public: enum { value = 0 }; }; template <typename T>
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
1
by: Lewis Baker | last post by:
I am having some template problems in VC7.1 The following code should compile fine but I get the error: 'warning C4667: 'void F(Traits<T>::P)' : no function template defined that matches...
2
by: desktop | last post by:
I have this code from the book C++ Templates: The Complete Guide: #ifndef ACCUM8_HPP_ #define ACCUM8_HPP_ #include "accumtraits4.hpp" #include "sumpolicy2.hpp"
5
by: Hong Ye | last post by:
Traits is a useful template technique to simplfy the implementation of some classes. however, I met some questions when I tried to understand and implement it. Following is an example of traits...
7
by: sheffmail | last post by:
I have the following code: template <class T> struct MyS { }; template<class T> inline const T& function(const std::locale& loc) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.