template dependent type

F

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?

Thanks in advance
Frank
 
A

Alf P. Steinbach

* 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
 
P

peter koch

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
 
F

Frank Neuhaus

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top