Is there any way to partially specialize on a class defining a specifictype

Discussion in 'C++' started by Pavel, Aug 21, 2010.

  1. Pavel

    Pavel Guest

    I am trying to make library code use a type specified by the library
    user, if he defines it in a typedef, and another type, if s/he doesn't,
    something as in the code below (which does not compile as it is).

    What's the simplest API for this from the perspective of library user?

    Ideally a user that does not need the flexibility of choosing his own
    Type does not have to pay for it in terms of API complexity.

    Thank in advance,
    -Pavel

    #include <iostream>

    using namespace std;

    template<typename T>
    struct TC // provided by library
    {
    static void DefaultF() { cout << "Default F()\n"; }
    };

    struct Concrete { /*...*/ }; // some Concretes are provided by library,
    some by user.

    template<>
    struct TC<Concrete>
    {
    static void ConcreteF() { cout << "Concrete F()\n"; }
    };


    struct HaveTypedef { typedef double Type; /* ... */ }; // provided by user

    template<typename T>
    struct TC<typename T::Type> {
    static void HaveTypedefF() { cout << "Have typedef F(): " <<
    T::Type() << '\n'; }
    };

    int
    main()
    {
    TC<int>::DefaultF();
    TC<double>::HaveTypedefF();
    TC<Concrete>::ConcreteF();

    return 0;
    }
     
    Pavel, Aug 21, 2010
    #1
    1. Advertising

  2. Re: Is there any way to partially specialize on a class defining a specific type

    Pavel wrote:

    > I am trying to make library code use a type specified by the library
    > user, if he defines it in a typedef, and another type, if s/he doesn't,
    > something as in the code below (which does not compile as it is).
    >
    > What's the simplest API for this from the perspective of library user?
    >
    > Ideally a user that does not need the flexibility of choosing his own
    > Type does not have to pay for it in terms of API complexity.
    >


    Sounds like you want to use SFINAE

    template<typename> struct tovoid {
    typedef void type;
    };

    template<typename T, typename = void>
    struct TC {
    /* generic ... */
    };

    template<typename T>
    struct TC<T, typename tovoid<typename T::Type> > {
    /* T has Type nested type */
    };
     
    Johannes Schaub (litb), Aug 21, 2010
    #2
    1. Advertising

  3. Pavel

    Pavel Guest

    Re: Is there any way to partially specialize on a class defininga specific type

    Johannes Schaub (litb) wrote:
    > Pavel wrote:
    >
    >> I am trying to make library code use a type specified by the library
    >> user, if he defines it in a typedef, and another type, if s/he doesn't,
    >> something as in the code below (which does not compile as it is).
    >>
    >> What's the simplest API for this from the perspective of library user?
    >>
    >> Ideally a user that does not need the flexibility of choosing his own
    >> Type does not have to pay for it in terms of API complexity.
    >>

    >
    > Sounds like you want to use SFINAE
    >
    > template<typename> struct tovoid {
    > typedef void type;
    > };
    >
    > template<typename T, typename = void>
    > struct TC {
    > /* generic ... */
    > };
    >
    > template<typename T>
    > struct TC<T, typename tovoid<typename T::Type> > {
    > /* T has Type nested type */
    > };
    >
    >

    Thanks Johannes -- this fits perfectly!

    -Pavel
     
    Pavel, Aug 22, 2010
    #3
    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. Old Wolf
    Replies:
    4
    Views:
    419
    Victor Bazarov
    Apr 8, 2005
  2. mrstephengross
    Replies:
    1
    Views:
    301
    Victor Bazarov
    Aug 2, 2005
  3. mathieu
    Replies:
    5
    Views:
    408
    Greg Herlihy
    May 15, 2008
  4. Replies:
    5
    Views:
    622
  5. Charles Comstock
    Replies:
    2
    Views:
    100
    Charles Comstock
    Apr 25, 2004
Loading...

Share This Page