Template traits

Discussion in 'C++' started by Aarti, Jul 13, 2007.

  1. Aarti

    Aarti Guest

    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;
    }
     
    Aarti, Jul 13, 2007
    #1
    1. Advertising

  2. Aarti

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 13, 2007
    #2
    1. Advertising

  3. Aarti

    Aarti Guest

    On Jul 12, 9:02 pm, Kai-Uwe Bux <> 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?
     
    Aarti, Jul 13, 2007
    #3
  4. Aarti

    Kai-Uwe Bux Guest

    Aarti wrote:

    > On Jul 12, 9:02 pm, Kai-Uwe Bux <> 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
     
    Kai-Uwe Bux, Jul 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. skscpp

    templates - container traits

    skscpp, Jul 3, 2003, in forum: C++
    Replies:
    0
    Views:
    710
    skscpp
    Jul 3, 2003
  2. Raider
    Replies:
    3
    Views:
    342
    Raider
    May 12, 2006
  3. Hong Ye
    Replies:
    5
    Views:
    376
    Naresh Rautela
    Jun 2, 2007
  4. ray
    Replies:
    1
    Views:
    1,335
    Robert Kern
    Jun 4, 2010
  5. Qi
    Replies:
    1
    Views:
    398
Loading...

Share This Page