[英]C++ determine the type of auto and pass to function template
I have a function : 我有一个功能:
template<typename T> f(T x) { do something with x; }
I want to pass this auto pointer into the function 我想将此自动指针传递给函数
auto x = ...
f<???>(x)
Is there anyway for me to do so ? 反正我有这样做吗?
Just call it like 像这样称呼它
auto x = ...
f(x)
templated functions automatically deduce the type depending on the arguments you pass it. 模板化函数会根据您传递给它的参数自动推断出类型。 In fact that's the preferred way to call a templated function.
实际上,这是调用模板函数的首选方法。
If you really want to explicitly give it the type (I don't recommend doing that) you can use decltype for it: 如果您确实要明确为其指定类型(我不建议这样做),则可以为其使用decltype:
auto x = ...
f<decltype(x)>(x)
Here a minimal proof: http://coliru.stacked-crooked.com/a/d01070d90c0b9803 这里是一个最小的证明: http : //coliru.stacked-crooked.com/a/d01070d90c0b9803
compiler should be smart enough to figure out the type, so 编译器应该足够聪明以找出类型,因此
auto x = ...
f(x);
or you can use decltype
或者您可以使用
decltype
auto x = ...
f<decltype(x)>(x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.