Stuck trying to do first exercise in C++ Template Metaprogramming

John (Eljay) Love-Jensen eljay@adobe.com
Mon Nov 16 12:45:00 GMT 2009


Hi Patrick,

To simplify the problem, what you are basically trying to do is this:

------------------------------
struct Foo 
{
  template <bool x>
  struct MyTemplate
  {
    enum { test = false };
  };  

  template <>
  struct MyTemplate<true>
  {
    enum { test = true };
  };  
};
------------------------------

The MyTemplate<true> template specialization cannot be done inside the Foo
declaration.  It is not valid C++.

Instead, you should do the template specialization outside of the declaring
outer struct (or in your case, outside of the outer template class
declaration) like this:

------------------------------
struct Foo
{
  template <bool x>
  struct MyTemplate
  {
    enum { test = false };
  };
};

template <>
struct Foo::MyTemplate<true>
{
  enum { test = true };
};
------------------------------

Hope that helps,
--Eljay




More information about the Gcc-help mailing list