Luk*_*ens 30 c++ types decltype c++11 type-deduction
我想自动推断出我正在编写的函数的返回类型.例:
std::vector<int> test(){
decltype(this_function) ret;
ret.push_back(5);
ret.push_back(9);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我所取得的成就是最好的
std::vector<int> test(){
decltype(test()) ret;
ret.push_back(5);
ret.push_back(9);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
这可以,但是:
如果我改变功能名称,我必须改变
decltype(试验())
成
decltype(名称())
如果我改变功能参数,我也必须改变
decltype(试验())
成
decltype(试验(参数1,参数2))
是否有更优雅的方式做同样的事情?
命名返回类型?
template <typename T=std::vector<int>>
T test(){
T ret;
ret.push_back(5);
ret.push_back(9);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
请不要让我限制这个enable_if.
怎么样 :
typedef std::vector<int> my_return_type;
my_return_type test(){
my_return_type ret;
ret.push_back(5);
ret.push_back(9);
return ret;
}
Run Code Online (Sandbox Code Playgroud)
我认为如果没有宏,你不会有更好的东西:)
| 归档时间: |
|
| 查看次数: |
7640 次 |
| 最近记录: |