c++ - 通过推导强制引用模板类型

标签 c++ templates c++14 language-lawyer

假设我有一个函数func:

template<typename T>
auto func(T arg){
    std::cout << std::boolalpha;
    std::cout << "T is ref: " << std::is_reference<T>::value << '\n';
}

有没有一种方法可以在不显式指定模板参数的情况下强制将 T 推导为引用类型?

就像能够写出像这样的东西:

auto main() -> int{
    auto x = 5;
    func(std::ref(x));
}

但不必专门针对 std::reference_wrapper

static_casting 不会阻止 int& 衰减为 Tint

假设我无法更改函数签名。

最佳答案

Imagine that I can not change the function signature.

签名

template<typename T>
auto func(T arg) { ... }

永远不会推导一个引用,因为类型推导适用于参数表达式的类型,并且来自 [expr]:

If an expression initially has the type “reference to T” (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis.

也就是说,模板推导只有在没有明确提供模板参数的情况下才会发生。所以你可以明确指定 T :

auto main() -> int{
    auto x = 5;
    func<int&>(x); // T = int&
}

否则,您可以在两者之间添加一个中间步骤:

template <typename T>
auto func_helper(T&& arg) {
    return func<T>(std::forward<T>(arg));
               ↑↑↑
}

因为,在 [temp.deduct.call] 中:

If P is a forwarding reference and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

所以如果func_helper使用左值调用时,模板参数 P 将被推导为引用。在你的例子中:

func_helper(x);

T将被推断为 int& ,所以我们显式地调用了与之前相同的函数:func<int&> .

func_helper(5);

T将被推断为 int , 我们会调用 func<int> , 如果我们调用 func 就会发生同样的情况直接地。

关于c++ - 通过推导强制引用模板类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31804946/

相关文章:

c++ - 如果您所做的只是等待它完成,为什么要使用异步?

C++:这种编译时多态性技术如何称呼?有什么优点和缺点?

c++ - 非模板类中的模板化 friend 类,其中 friend 也使用该类

c++ - 使用部分特化从 boost:hana::set 中提取类型失败

c++ - 模仿 std::function 模板参数

C++:从捕获函数参数的函数返回lambda表达式

c++ - 如何制作 set<T>(args...) 方法?

c++ - initializer_list : No such file or directory

c++ - 如何最好地比较两个编译的二进制文件?

C++ 静态成员在初始化后重新初始化