template dependent type

Discussion in 'C++' started by Frank Neuhaus, May 9, 2009.

  1. Hi,
    say I have the following situation:

    struct Foo
    {
    static int something();
    }

    struct Bar
    {
    static float something();
    }

    template<typename T> void func()
    {
    ??? x=T::something();
    };

    func<Foo>();
    func<Bar>();

    Is there anything I can use as ??? to make x of the same type that
    T::something returns? So far I believe that the only option is to use an
    additional typedef in each struct... The C++0x 'auto' feature appears to
    solve this issue. But is there also anything in the current C++ standard?

    Thanks in advance
    Frank
     
    Frank Neuhaus, May 9, 2009
    #1
    1. Advertising

  2. * Frank Neuhaus:
    > Hi,
    > say I have the following situation:
    >
    > struct Foo
    > {
    > static int something();
    > }
    >
    > struct Bar
    > {
    > static float something();
    > }
    >
    > template<typename T> void func()
    > {
    > ??? x=T::something();
    > };
    >
    > func<Foo>();
    > func<Bar>();
    >
    > Is there anything I can use as ??? to make x of the same type that
    > T::something returns? So far I believe that the only option is to use an
    > additional typedef in each struct... The C++0x 'auto' feature appears to
    > solve this issue. But is there also anything in the current C++ standard?


    The Boost library has a result type metafunction (template thing).


    Cheers & hth.,

    - Alf

    --
    Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
    No ads, and there is some C++ stuff! :) Just going there is good. Linking
    to it is even better! Thanks in advance!
     
    Alf P. Steinbach, May 9, 2009
    #2
    1. Advertising

  3. Frank Neuhaus

    peter koch Guest

    On 9 Maj, 20:32, "Frank Neuhaus" <> wrote:
    > Hi,
    > say I have the following situation:
    >
    > struct Foo
    > {
    > static int something();
    >
    > }
    >
    > struct Bar
    > {
    > static float something();
    >
    > }
    >
    > template<typename T> void func()
    > {
    >   ??? x=T::something();
    >
    > };
    >
    > func<Foo>();
    > func<Bar>();
    >
    > Is there anything I can use as ??? to make x of the same type that
    > T::something returns? So far I believe that the only option is to use an
    > additional typedef in each struct... The C++0x 'auto' feature appears to
    > solve this issue. But is there also anything in the current C++ standard?
    >
    > Thanks in advance
    >    Frank


    No. I believe you would have to augment the used class with the proper
    typedefinition, e.g.
    class Foo
    {
    typedef int returnedtype;
    ....

    and use that type in your template.
    As an alternative, you could check boost - it dims that they might
    have some magic, that might solve your problems. I just checked and
    there's a library called typeof that might fullfill your needs.

    /Peter
     
    peter koch, May 9, 2009
    #3
  4. > and use that type in your template.
    > As an alternative, you could check boost - it dims that they might
    > have some magic, that might solve your problems. I just checked and
    > there's a library called typeof that might fullfill your needs.


    Cool. At first I thought I'd have to use function_traits as Alf suggested.
    But it seems that BOOST_AUTO is alot better suited to what I want to do.

    Thanks :)
     
    Frank Neuhaus, May 9, 2009
    #4
    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. pervinder
    Replies:
    4
    Views:
    3,082
    Malte Starostik
    Mar 29, 2005
  2. Nate Barney

    dependent template parameter

    Nate Barney, Sep 15, 2006, in forum: C++
    Replies:
    2
    Views:
    366
    Nate Barney
    Sep 15, 2006
  3. er
    Replies:
    1
    Views:
    356
  4. Sanil
    Replies:
    2
    Views:
    4,856
    James Kanze
    Dec 14, 2007
  5. puzzlecracker
    Replies:
    1
    Views:
    549
    James Kanze
    Aug 7, 2008
Loading...

Share This Page