Search code examples
c++c++11return-typedecltypetrailing-return-type

Type forwarding in C++11


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)


Solution

  • 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.