Run-time check for template function existance

Discussion in 'C++' started by Patrick Kowalzick, Aug 27, 2003.

  1. Hello all,

    I want to test run-time if a template function for a special type exists.
    See example below. The checkfoo function is where I need some hints.

    Regards,
    Patrick


    #include <iostream>

    template <typename> struct fooclass;

    template <> struct fooclass<int>
    {
    static void huhu()
    {
    std::cout << "int" << std::endl;
    }
    };

    template <> struct fooclass<long>
    {
    static void huhu()
    {
    std::cout << "long" << std::endl;
    }
    };

    template <typename T> void foo()
    {
    fooclass<T>::huhu();
    }

    template <typename T> bool checkfoo()
    {
    if ( foo<T>() ) return true; // check for existance here
    else return false;
    }


    int main()
    {
    foo<int>();
    foo<long>();

    checkfoo<int>(); // should return true
    checkfoo<double>(); // should return false

    return 0;
    }
     
    Patrick Kowalzick, Aug 27, 2003
    #1
    1. Advertisements

  2. Patrick Kowalzick

    Chris Theis Guest

    "Patrick Kowalzick" <> wrote in message
    news:bii7od$cv8$...
    > Hello all,
    >
    > I want to test run-time if a template function for a special type exists.
    > See example below. The checkfoo function is where I need some hints.
    >
    > Regards,
    > Patrick
    >

    [SNIP]

    Hi Patrick,

    the whole thing is not as trivial as it might seem. In principle the
    compiler looks for instantiations of templates for a specific type and
    writes code for this type. This means that there will only be code for types
    that are actually used. Actually that's one of the big advantages of
    templates because it will prevent code - bloating. Also in the context of
    incomplete instatiation one sees the true power of this concept, though that
    would lead too far here.

    Anyway this means that there is no easy way to do the checking during
    runtime but you could do it during compile time. IMHO compile time checks
    are anyway better than runtime checks because it's up to the implementer to
    decide how/what to fix but the runtime error will occur at the client side
    :)
    If you still want to stick to runtime checks you can register all your
    functions in a map and check whether an appropriate function is available.

    Hope that helps
    Chris
     
    Chris Theis, Aug 27, 2003
    #2
    1. Advertisements

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. Ajit
    Replies:
    5
    Views:
    1,227
    Nicolas
    Sep 26, 2003
  2. Imran Aziz

    Check existance of a querystring variable

    Imran Aziz, Aug 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    30,910
    Imran Aziz
    Aug 9, 2005
  3. google khan

    Check Existance of Bean in Struts

    google khan, Nov 30, 2006, in forum: Java
    Replies:
    1
    Views:
    945
    Manish Pandit
    Nov 30, 2006
  4. Imre
    Replies:
    2
    Views:
    395
    mlimber
    May 25, 2006
  5. Dan
    Replies:
    15
    Views:
    803
    Patrice
    Jul 9, 2007
  6. Replies:
    2
    Views:
    648
    Kai-Uwe Bux
    Oct 23, 2007
  7. Pierre Yves
    Replies:
    2
    Views:
    761
    Pierre Yves
    Jan 10, 2008
  8. Cirene
    Replies:
    1
    Views:
    485
    sloan
    Aug 26, 2008
Loading...