Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use decltype without any instance variables?

Tags:

c++

decltype

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?

like image 319
Jim Hunziker Avatar asked Feb 02 '26 15:02

Jim Hunziker


1 Answers

This is exactly what std::declval is for:

decltype(the_function(std::declval<Type1>(), std::declval<Type2>())) return_value;
like image 80
Quentin Avatar answered Feb 05 '26 05:02

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!