Reputation: 1470
Following situation:
class CTest
{
public:
CTest()=default;
~CTest()=default;
auto SomeFunc_Helper(std::integral_constant<int, 8> param) -> uint64_t*; //param is in reality more or less a self-implemented std::integral_constant
auto SomeFunc() -> [how do I get the return type of SomeFunc_Helper?]
{
return SomeFunc_Helper(std::integral_constant<int, sizeof(uintptr_t)>{});
}
};
For SomeFunc()
I tried something like
auto SomeFunc() ->decltype(&CTest::SomeFunc_Helper(std::integral_constant<int, 8>))
gives me the error that std::integral_constant<int, 8>
could not be resolved.
So my question would be how one would do a type-forwarding from one function to another function?
(Solutions up to C++11 excluding the std::
namespace are welcome)
Upvotes: 2
Views: 144
Reputation: 275220
This is one of the cases where a macro is worthwhile.
#define RETURNS(...) \
noexcept(noexcept(__VA_ARGS__)) \
-> decltype(__VA_ARGS__) \
{ return __VA_ARGS__; }
now you can just:
auto SomeFunc()
RETURNS( SomeFunc_Helper(std::integral_constant<int, sizeof(uintptr_t)>{}) )
There has been a proposal that RETURNS
equivalent functionality will become =>
or somesuch in lambdas; I hope it will be generalized.
Upvotes: 3
Reputation: 1933
I think you are looking for std::result_of
, however, in this case, you should be fine with only declaring the return type as decltype(auto)
(since C++14):
auto SomeFunc() -> decltype(auto)
{
return SomeFunc_Helper(std::integral_constant<int, 8>{});
}
This way, if the function returns a reference, for example, you perfectly forward the reference to the SomeFunction
, too.
Upvotes: 2
Reputation: 38267
You can try this:
auto SomeFunc() -> decltype(SomeFunc_Helper(std::integral_constant<int, 8>()))
{
/* ... */
}
The argument to decltype
in the trailing return type can be any valid expression, in this case, the exact call to the member function that is actually performed in the function body.
Upvotes: 2