在 C++11 中键入转发
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>
的错误。 所以我的问题是如何将从一个函数转发到另一个函数? (欢迎最多 C++11 的解决方案(不包括std::
命名空间(
这是宏值得的情况之一。
#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
等效功能将在 lambda 中变得=>
或类似;我希望它能被概括。
你可以试试这个:
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
。
相关文章:
- MSVC是否支持C++11样式的属性而不是__declspec
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 正在折叠转发引用
- 如何将模板转换为C++11之前的模板
- c++11评估顺序(未定义的行为)
- C++中的VLA,扩展名为std=C++11
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在 C++11 中键入转发
- C++11 重载方法,并转发到唯一方法
- 我如何实现变异模式以将可变数量的参数转发到C 11中的函数
- 将C 11设置功能更改为带有转发的现代模板功能是一个好主意
- C++11可变模板函数调用转发
- C++11 标准::功能和完美的转发
- 如何在 C++11 中使用可变参数模板正确创建显式参数转发函数
- C++11 完美的转发和引用折叠
- C++11:完美转发中的恒常性
- C++11:std的微妙之处::转发:身份真的有必要吗?
- C++11虚拟模板方法或转发可变模板