Jim Hunziker
Jim Hunziker

Reputation: 15380

Can I use decltype without any instance variables?

There is a function I'm using from a library that has a macro-ridden output type:

STRANGE_MACRO(something) the_function(Type1 t, Type2 u);

I would like to define a variable that will take this return value without first declaring a Type1 or a Type2.

I was hoping something like this would work:

decltype(the_function(Type1, Type2)) return_value;

But it doesn't work. Can I do this without figuring out what the macro evaluates to and without declaring a couple variables first?

Upvotes: 3

Views: 396

Answers (1)

Quentin
Quentin

Reputation: 63124

This is exactly what std::declval is for:

decltype(the_function(std::declval<Type1>(), std::declval<Type2>())) return_value;

Upvotes: 10

Related Questions