c++ - 定义与变量 a 相同类型的变量 b

标签 c++ templates c++11 traits

是否可以声明与另一个变量var_a具有相同类型的变量var_b

例如:

template <class T>
void foo(T t) {

   auto var_a = bar(t);
   //make var_b of the same type as var_a

}


F_1 bar(T_1 t) {

}

F_2 bar(T_2 t) {

}

最佳答案

当然,使用 decltype:

auto var_a = bar(t);
decltype(var_a) b;

您可以添加 cv 限定符和对 decltype 说明符的引用,就像它是任何其他类型一样:

const decltype(var_a)* b;

关于c++ - 定义与变量 a 相同类型的变量 b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393546/

相关文章:

c++ - 已弃用的 std::auto_ptr 的 c++11 标准等价物是什么?

c++ - 为什么 std::is_array 对 std::array 返回 false?

c++ - 在 C++ 中计算毫秒的可移植方法?

c++ - 移动时略有停顿/延迟

spring - JSP 是否被取代,如果是,如何被取代?

c++ - 检测类型何时不需要调用其析构函数

c++ - 在 C++11 中使用模板元编程连接列表

c++ - 创建具有有限参数的 std::function 类型

c++ - spirit real_parser ".e"

c++ - 在 c++14 lambda 表达式中捕获和移动 unique_ptr