Template traits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aarti

    #1

    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
    'isOptimizedImp lAvailable'. Any pointers would be of great help.
    Here is my code

    #include <iostream>

    using namespace std;

    template<typena me 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<typena me T>
    class foo
    {
    public:
    typename foo_traits<T>:: customImpl isOptimizedImpl Available()
    {
    return foo_traits<T>:: customImpl;
    }
    };



    int main()
    {
    foo<intf;
    }

  • Kai-Uwe Bux

    #2
    Re: Template traits

    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
    'isOptimizedImp lAvailable'. Any pointers would be of great help.
    Here is my code
    >
    #include <iostream>
    >
    using namespace std;
    >
    template<typena me 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<typena me T>
    class foo
    {
    public:
    typename foo_traits<T>:: customImpl isOptimizedImpl Available()
    bool isOptimizedImpl Available()
    {
    return foo_traits<T>:: customImpl;
    }
    };
    >
    >
    >
    int main()
    {
    foo<intf;
    }

    Best

    Kai-Uwe Bux

    Comment

    • Aarti

      #3
      Re: Template traits

      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
      'isOptimizedImp lAvailable'. Any pointers would be of great help.
      Here is my code
      >
      #include <iostream>
      >
      using namespace std;
      >
      template<typena me 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<typena me T>
      class foo
      {
      public:
      typename foo_traits<T>:: customImpl isOptimizedImpl Available()
      >
      bool isOptimizedImpl Available()
      >
      {
      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?

      Comment

      • Kai-Uwe Bux

        #4
        Re: Template traits

        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
        'isOptimizedImp lAvailable'. Any pointers would be of great help.
        Here is my code
        >>
        #include <iostream>
        >>
        using namespace std;
        >>
        template<typena me 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<typena me T>
        class foo
        {
        public:
        typename foo_traits<T>:: customImpl isOptimizedImpl Available()
        >>
        >bool isOptimizedImpl Available()
        >>
        {
        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

        Comment

        Working...