specialized template function w/compiler error on invalid type

Discussion in 'C++' started by chrisstankevitz@yahoo.com, Nov 9, 2006.

  1. Guest

    Hi,

    Q1: Is there a way to make a template function that works only for
    specific types which produces a compiler error if used with an invalid
    type?
    Q2: If not, how do people deal with this issue?

    I believe A1 is no. I tried below with template function "f".

    For A2, functors work but have ugly syntax and runtime overhead. A
    dispatcher to the funtor has nice syntax but still the overhead. I'm
    just guessing about the overhead, but I believe there is a ctor.

    Thanks,

    Chris

    //==========


    // f: template function defined only for char
    template<typename T>
    void f(T t);

    template<>
    void f(char i)
    {
    // ...
    }

    // g: template functor defined only for char
    template<typename T>
    struct g;

    template<>
    struct g<char>
    {
    void operator()(char i)
    {
    // ...
    }
    };

    // h: dispatcher to g
    template<typename T>
    void h(T t)
    {
    g<T>()(t);
    }

    int main()
    {
    char c;
    long l;

    // f - template function w/specialization
    // syntax: clean
    // runtime: fast
    // compiler error on invalid type: no (but linker error)
    f(c);
    f(l);

    // g - template class w/specialization
    // syntax: ugly
    // runtime: slow
    // compiler error on invalid type: yes
    g<char>()(c);
    g<long>()(l);

    // h - Dispatcher function calling g
    // syntax: clean
    // runtime: slow
    // compiler error on invalid type: yes (albeit cryptic)
    h(c);
    h(l);

    return 0;
    }
    , Nov 9, 2006
    #1
    1. Advertising

  2. * :
    > Hi,
    >
    > Q1: Is there a way to make a template function that works only for
    > specific types which produces a compiler error if used with an invalid
    > type?


    Yes. The general technique is to use SFINAE. The Boost library
    provides ready-to-use helper classes for this.


    > Q2: If not, how do people deal with this issue?


    N/A.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 9, 2006
    #2
    1. Advertising

  3. wrote:
    > Q1: Is there a way to make a template function that works only for
    > specific types which produces a compiler error if used with an invalid
    > type?


    I am sorry, but this is just silly. Why do you need it to be a template
    then? Drop the type from the template, replace it with your specific
    type (for which you need it work) and be happy.

    > Q2: If not, how do people deal with this issue?


    Normal people don't. See above.

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 10, 2006
    #3
  4. Guest

    Victor Bazarov wrote:
    > Why do you need it to be a template


    I use the template facility to use a specialized implementation for 64
    bit long. Overloading cannot do this AFAIK.
    , Nov 10, 2006
    #4
  5. wrote:
    > Victor Bazarov wrote:
    >> Why do you need it to be a template

    >
    > I use the template facility to use a specialized implementation for 64
    > bit long. Overloading cannot do this AFAIK.


    Just disregard my message; I failed to notice plural in your "types"
    requirement. Sorry. I've already cancelled it...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 10, 2006
    #5
  6. Guest

    Alf P. Steinbach wrote:
    > use SFINAE.


    Wow, thanks. Google search showed me some amazing stuff.

    Chris
    , Nov 10, 2006
    #6
    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.

Share This Page