implementation: specialization of member of template class

Discussion in 'C++' started by vlad.k.sm@gmail.com, Mar 3, 2009.

  1. Guest

    Specialization of member of template class is impossible according to C
    ++ standard. The following code:

    template<typename T> struct enbl_if {};
    template<> struct enbl_if<int> { typedef int type; };

    template<typename T> struct disble_if {typedef T type; };
    template<> struct disble_if<int> {};

    template<typename T>
    struct test
    {
    template<typename T2>
    typename enbl_if<T2>::type get() const
    {
    std::cout << "enbl_if" << std::endl;
    return T2();
    }

    template<typename T2>
    typename disble_if<T2>::type get() const
    {
    std::cout << "disble_if" << std::endl;
    return T2();
    }
    };


    int main()
    {
    int i = test<int>().get<int>();
    long l = test<int>().get<long>();
    std::string s = test<int>().get<std::string>();
    return 0;
    }

    prints:
    enbl_if
    disble_if
    disble_if

    and so, it works as full specialization for "int". I was able to
    compile it under gcc 4.2, VC2003-2005, comeua online. Is it correct
    code?
    p.s.: instead enbl_if, disble_if need to use appropriate boost utility
    in complete code.
    , Mar 3, 2009
    #1
    1. Advertising

  2. Guest

    On Mar 3, 10:55 pm, Victor Bazarov <> wrote:
    > wrote:
    > > Specialization of member of template class is impossible according to C
    > > ++ standard. The following code:

    >
    > > template<typename T> struct enbl_if {};
    > > template<> struct enbl_if<int> { typedef int type; };

    >
    > > template<typename T> struct disble_if {typedef T type; };
    > > template<> struct disble_if<int> {};

    >
    > > template<typename T>
    > > struct test
    > > {
    > >    template<typename T2>
    > >    typename enbl_if<T2>::type get() const
    > >    {
    > >       std::cout << "enbl_if" << std::endl;
    > >       return T2();
    > >    }

    >
    > >    template<typename T2>
    > >    typename disble_if<T2>::type get() const
    > >    {
    > >       std::cout << "disble_if" << std::endl;
    > >       return T2();
    > >    }
    > > };

    >
    > > int main()
    > > {
    > >    int i = test<int>().get<int>();
    > >    long l = test<int>().get<long>();
    > >    std::string s = test<int>().get<std::string>();
    > >    return 0;
    > > }

    >
    > > prints:
    > > enbl_if
    > > disble_if
    > > disble_if

    >
    > > and so, it works as full specialization for "int". I was able to
    > > compile it under gcc 4.2, VC2003-2005, comeua online. Is it correct
    > > code?

    >
    > Seems OK.  Why are you concerned?  It would seem that you heard that
    > "specialisation is not allowed" but are not sure whether it applies
    > here.  Actually, what's not allowed is to declare a specialisation of a
    > template member without specialising the template.  You're not declaring
    > any specialisations here.
    >

    I am concerned because this isn't a valid overloading if remove
    enable_if/disable_if.
    , Mar 5, 2009
    #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. Dave
    Replies:
    4
    Views:
    7,651
    pdixtl
    Jun 4, 2010
  2. Ruben Campos
    Replies:
    3
    Views:
    6,451
  3. BigMan
    Replies:
    1
    Views:
    521
  4. Joseph Turian
    Replies:
    4
    Views:
    570
    John Carson
    Mar 20, 2006
  5. toton
    Replies:
    2
    Views:
    323
    toton
    Feb 22, 2008
Loading...

Share This Page