Partial but not full/explicit specialization at non-namespace scope.

Discussion in 'C++' started by Ferdi Smit, Oct 19, 2005.

  1. Ferdi Smit

    Ferdi Smit Guest

    I've never understood the rationale of allowing partial, but not
    explicit specialization for classes at non-namespace scope. Ie.:

    struct A {
    template <typename T1, typename T2>
    struct B {};

    // this is not allowed:
    template <>
    struct B<int, float> {};

    // but this IS:
    template <typename T2>
    struct B<int, T2> {};
    };

    What is the reason for this rule?
    Ferdi Smit, Oct 19, 2005
    #1
    1. Advertising

  2. Ferdi Smit

    Dan Cernat Guest

    Ferdi Smit wrote:
    > I've never understood the rationale of allowing partial, but not
    > explicit specialization for classes at non-namespace scope. Ie.:
    >
    > struct A {
    > template <typename T1, typename T2>
    > struct B {};
    >
    > // this is not allowed:
    > template <>
    > struct B<int, float> {};
    >
    > // but this IS:
    > template <typename T2>
    > struct B<int, T2> {};
    > };
    >
    > What is the reason for this rule?


    VC++ 7.1 the code below compiles

    struct A {
    template <typename T1, typename T2>
    struct B {};

    template <>
    struct B<int, float> {};

    template <typename T2>
    struct B<int, T2> {};
    };

    int main()
    {
    A a;
    A::B<char, char> b1;
    A::B<int, float> b2;
    A::B<int, char> b3;

    return 0;
    }

    so I guess it is time to change compilers.

    dan
    Dan Cernat, Oct 19, 2005
    #2
    1. Advertising

  3. Re: Partial but not full/explicit specialization at non-namespacescope.

    Ferdi Smit wrote:
    > I've never understood the rationale of allowing partial, but not
    > explicit specialization for classes at non-namespace scope. Ie.:
    >
    > struct A {
    > template <typename T1, typename T2>
    > struct B {};
    >
    > // this is not allowed:
    > template <>
    > struct B<int, float> {};
    >
    > // but this IS:
    > template <typename T2>
    > struct B<int, T2> {};
    > };
    >
    > What is the reason for this rule?
    >


    I strongly recommend asking about rationales in comp.std.c++. Here we
    talk the "how", there they discuss the "why".

    V
    Victor Bazarov, Oct 19, 2005
    #3
  4. Ferdi Smit

    Greg Comeau Guest

    In article <>,
    Dan Cernat <> wrote:
    >Ferdi Smit wrote:
    >> I've never understood the rationale of allowing partial, but not
    >> explicit specialization for classes at non-namespace scope. Ie.:
    >>
    >> struct A {
    >> template <typename T1, typename T2>
    >> struct B {};
    >>
    >> // this is not allowed:
    >> template <>
    >> struct B<int, float> {};
    >>
    >> // but this IS:
    >> template <typename T2>
    >> struct B<int, T2> {};
    >> };
    >>
    >> What is the reason for this rule?

    >
    >VC++ 7.1 the code below compiles
    >
    >struct A {
    > template <typename T1, typename T2>
    > struct B {};
    >
    > template <>
    > struct B<int, float> {};
    >
    > template <typename T2>
    > struct B<int, T2> {};
    >};
    >
    >int main()
    >{
    > A a;
    > A::B<char, char> b1;
    > A::B<int, float> b2;
    > A::B<int, char> b3;
    >
    > return 0;
    >}
    >
    >so I guess it is time to change compilers.


    Comeau C++ will also compile the above code.

    However, it will only do it in our VC++ compatibility mode,
    because in standard mode it is an error:

    Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
    Copyright 1988-2005 Comeau Computing. All rights reserved.
    MODE:strict errors C++

    "it2.cpp", line 5: error: explicit specialization is not allowed in the
    current scope
    template <>
    ^

    This is as per Standard C++, since that line can't be in class scope.

    So I guess it is time to change compilers :)
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 19, 2005
    #4
  5. Ferdi Smit

    Dan Cernat Guest

    Greg Comeau wrote:
    > In article <>,
    > Dan Cernat <> wrote:
    > >Ferdi Smit wrote:

    [snip]

    > >
    > >VC++ 7.1 the code below compiles
    > >

    [snip again]

    > >so I guess it is time to change compilers.

    >
    > Comeau C++ will also compile the above code.
    >
    > However, it will only do it in our VC++ compatibility mode,
    > because in standard mode it is an error:
    >
    > Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
    > Copyright 1988-2005 Comeau Computing. All rights reserved.
    > MODE:strict errors C++
    >
    > "it2.cpp", line 5: error: explicit specialization is not allowed in the
    > current scope
    > template <>
    > ^
    >
    > This is as per Standard C++, since that line can't be in class scope.
    >
    > So I guess it is time to change compilers :)


    LOL

    Thanks, Greg.
    Dan Cernat, Oct 19, 2005
    #5
  6. Ferdi Smit

    Greg Comeau Guest

    In article <>,
    Dan Cernat <> wrote:
    >
    >Greg Comeau wrote:
    >> In article <>,
    >> Dan Cernat <> wrote:
    >> >Ferdi Smit wrote:

    >[snip]
    >
    >> >
    >> >VC++ 7.1 the code below compiles
    >> >

    > [snip again]
    >
    >> >so I guess it is time to change compilers.

    >>
    >> Comeau C++ will also compile the above code.
    >>
    >> However, it will only do it in our VC++ compatibility mode,
    >> because in standard mode it is an error:
    >>
    >> Comeau C/C++ 4.3.4.1 (Mar 30 2005 22:54:12) for MS_WINDOWS_x86
    >> Copyright 1988-2005 Comeau Computing. All rights reserved.
    >> MODE:strict errors C++
    >>
    >> "it2.cpp", line 5: error: explicit specialization is not allowed in the
    >> current scope
    >> template <>
    >> ^
    >>
    >> This is as per Standard C++, since that line can't be in class scope.
    >>
    >> So I guess it is time to change compilers :)

    >
    >LOL
    >
    >Thanks, Greg.


    BTW, the reason, at least as I recall it (so I could be wrong
    and/or recalling it wrong), was "simply" that it was not known if
    allowing it that way would be beneficial, and since allowing
    a more liberal structure was considered to be a possible
    implementation burden upon implementators, it was prohibited.
    It's come up before about relaxing that, especially in combo
    with other prohibitions.
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
    Greg Comeau, Oct 19, 2005
    #6
  7. Ferdi Smit

    Ferdi Smit Guest

    "Dan Cernat" <> wrote in message
    news:...
    >
    >
    > VC++ 7.1 the code below compiles
    > [snip]
    > so I guess it is time to change compilers.
    >


    Yes, I know, perhaps I ought to have mentioned that. Anyway, not trying to
    bash anyone, but VC7.1 is not a very good compiler to test these matters
    with. I've found numerous of similar, wrong "features" concerning templates;
    even in strict mode. But thanks for your time in trying to compile it for
    me, it's appreciated :)

    --
    Ferdi Smit
    Ferdi Smit, Oct 19, 2005
    #7
  8. Ferdi Smit

    Ferdi Smit Guest

    "Greg Comeau" <> wrote in message
    news:dj60k4$9p5$...
    > In article <>,
    > BTW, the reason, at least as I recall it (so I could be wrong
    > and/or recalling it wrong), was "simply" that it was not known if
    > allowing it that way would be beneficial, and since allowing
    > a more liberal structure was considered to be a possible
    > implementation burden upon implementators, it was prohibited.
    > It's come up before about relaxing that, especially in combo
    > with other prohibitions.


    But isn't partial specialization the more liberal structure? Whenever I
    really need explicit specialization in this way, I can simply add a dummy
    template parameter :

    struct A {
    template <typename T, int dummy>
    struct B {};

    // this is ok, simulating explicit spec. with dummy
    template <int dummy>
    struct B<float, dummy> {};

    // then why is this (or similar without the dummy) not ok?
    template <>
    struct B<float, 0> {};
    };

    I've used this to write templated recursion on an integer in the class. As
    (member) functions can only be explicitly specialized, adding a dummy
    template parameter is not an option there. Another way out is to use an
    Int2Type construct (ala alexandrescu/loki) and function overloading...
    however, why do I have to resort to these kinds of tricks? To me it doesn't
    really make sense, however I might be missing a deeper issue (?)

    --
    Ferdi Smit
    Ferdi Smit, Oct 19, 2005
    #8
  9. Ferdi Smit

    Greg Guest

    Ferdi Smit wrote:
    > "Greg Comeau" <> wrote in message
    > news:dj60k4$9p5$...
    > > In article <>,
    > > BTW, the reason, at least as I recall it (so I could be wrong
    > > and/or recalling it wrong), was "simply" that it was not known if
    > > allowing it that way would be beneficial, and since allowing
    > > a more liberal structure was considered to be a possible
    > > implementation burden upon implementators, it was prohibited.
    > > It's come up before about relaxing that, especially in combo
    > > with other prohibitions.

    >
    > But isn't partial specialization the more liberal structure? Whenever I
    > really need explicit specialization in this way, I can simply add a dummy
    > template parameter :
    >
    > struct A {
    > template <typename T, int dummy>
    > struct B {};
    >
    > // this is ok, simulating explicit spec. with dummy
    > template <int dummy>
    > struct B<float, dummy> {};
    >
    > // then why is this (or similar without the dummy) not ok?
    > template <>
    > struct B<float, 0> {};
    > };
    >
    > I've used this to write templated recursion on an integer in the class. As
    > (member) functions can only be explicitly specialized, adding a dummy
    > template parameter is not an option there. Another way out is to use an
    > Int2Type construct (ala alexandrescu/loki) and function overloading...
    > however, why do I have to resort to these kinds of tricks? To me it doesn't
    > really make sense, however I might be missing a deeper issue (?)


    All that is missing is a namespace. Declaring A in a namespace lets B
    be explicitly instantiated:

    namespace N {

    struct A
    {
    template <typename T1, typename T2>
    struct B
    {
    };
    };

    // explicit instantiation for N::A::B<int, int>
    template <>
    struct A::B<int, float>
    {
    }; }

    using N::A;

    int main()
    {
    A::B<int, float> a;
    }

    Why A has to be in a namespace in order for B to be explicitly
    instantiated is a bit unclear. But as this example shows, the effect on
    the program is otherwise unlikely to be dramatic. And declaring classes
    in non-global namespaces is usually a good idea for other reasons.

    Greg
    Greg, Oct 19, 2005
    #9
    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. Patrick Kowalzick
    Replies:
    0
    Views:
    796
    Patrick Kowalzick
    Oct 29, 2004
  2. Andy
    Replies:
    5
    Views:
    490
    Shezan Baig
    Jan 30, 2005
  3. Kai-Uwe Bux
    Replies:
    7
    Views:
    304
    Michael Kochetkov
    Oct 8, 2005
  4. Bill
    Replies:
    7
    Views:
    380
    Marcel Müller
    Feb 22, 2008
  5. Bas
    Replies:
    3
    Views:
    514
Loading...

Share This Page