Template specializations and namespaces

Discussion in 'C++' started by Juha Nieminen, Dec 12, 2011.

  1. I'm wondering why this seems to be illegal C++:

    namespace Hops { template<bool> void foo(); }
    template<> void Hops::foo<false>() { std::cout << "false\n"; }
    template<> void Hops::foo<true>() { std::cout << "true\n"; }

    Both gcc and clang give errors, saying that the template specializations
    are in a "different namespace" than the template declaration.

    (Yes, I know that the problem can be solved by putting the
    specializations explicitly inside the namespace. However, this seems
    to be an odd special rule. Compilers do not seem to have any problem
    in compiling this if you enable a proper extension. Why does this rule
    exist?)
    Juha Nieminen, Dec 12, 2011
    #1
    1. Advertising

  2. On Dec 12, 3:23 pm, Juha Nieminen <> wrote:
    >   I'm wondering why this seems to be illegal C++:
    >
    > namespace Hops { template<bool> void foo(); }
    > template<> void Hops::foo<false>() { std::cout << "false\n"; }
    > template<> void Hops::foo<true>() { std::cout << "true\n"; }
    >
    >   Both gcc and clang give errors, saying that the template specializations
    > are in a "different namespace" than the template declaration.
    >
    >   (Yes, I know that the problem can be solved by putting the
    > specializations explicitly inside the namespace. However, this seems
    > to be an odd special rule. Compilers do not seem to have any problem
    > in compiling this if you enable a proper extension. Why does this rule
    > exist?)


    http://gcc.gnu.org/onlinedocs/gcc/Name-lookup.html
    Andrew Tomazos, Dec 13, 2011
    #2
    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. Tseng, Ling-hua
    Replies:
    0
    Views:
    435
    Tseng, Ling-hua
    Dec 8, 2003
  2. Tom Richardson
    Replies:
    1
    Views:
    323
    Victor Bazarov
    Dec 19, 2003
  3. Thomas Matthews

    Order of template specializations

    Thomas Matthews, Feb 26, 2004, in forum: C++
    Replies:
    0
    Views:
    333
    Thomas Matthews
    Feb 26, 2004
  4. Imre
    Replies:
    1
    Views:
    288
    Chris Jefferson
    Feb 21, 2005
  5. Samee Zahur
    Replies:
    1
    Views:
    353
    Victor Bazarov
    Apr 29, 2005
Loading...

Share This Page