[英]Type forwarding in C++11
以下情况:
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)>{});
}
};
对于SomeFunc()
我试过类似的东西
auto SomeFunc() ->decltype(&CTest::SomeFunc_Helper(std::integral_constant<int, 8>))
给出了无法解析std::integral_constant<int, 8>
的错误。 所以我的问题是如何进行从一个函数到另一个函数的类型转发? (欢迎使用除了std::
namespace之外的C ++ 11解决方案)
这是宏观值得的情况之一。
#define RETURNS(...) \
noexcept(noexcept(__VA_ARGS__)) \
-> decltype(__VA_ARGS__) \
{ return __VA_ARGS__; }
现在你可以:
auto SomeFunc()
RETURNS( SomeFunc_Helper(std::integral_constant<int, sizeof(uintptr_t)>{}) )
有一个提议, RETURNS
等效功能将成为=>
或lambdas中的某些功能; 我希望它会被推广。
你可以试试这个:
auto SomeFunc() -> decltype(SomeFunc_Helper(std::integral_constant<int, 8>()))
{
/* ... */
}
尾部返回类型中decltype
的参数可以是任何有效的表达式,在本例中,是对函数体中实际执行的成员函数的完全调用。
我认为你正在寻找std::result_of
,但是,在这种情况下,只应将返回类型声明为decltype(auto)
(因为C ++ 14),你应该没问题:
auto SomeFunc() -> decltype(auto)
{
return SomeFunc_Helper(std::integral_constant<int, 8>{});
}
这样,如果函数返回引用,例如,您也完全转发对SomeFunction
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.