C++调用时指定函数的非模板版本

标签 c++ templates

我想知道,有没有办法强制调用非模板函数,比如:

template <class T>
void foo(T&);

void foo(const int&);

void bar()
{
   int a;
   foo(a); // templated version is called, not a usual function
}

最佳答案

你可以

foo(const_cast<const int&>(a));

foo(static_cast<const int&>(a));

或通过中间变量

const int& crefa = a;
foo(crefa);

或包装器:

foo(std::cref(a));

或者指定foo:

static_cast<void(&)(const int&)>(foo)(a);

关于C++调用时指定函数的非模板版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741143/

相关文章:

c++ - 为什么模板非类型参数不能是类类型

c++ - 使用外部 *.lib 文件进行 Qt 部署

c++ - 如何在Boost MultiIndex中找到最常见的非唯一键?

c++ - 放置 new[] 的开销

c++ - 程序在Turbo C中运行良好,但在MSVC中失败,出现未处理的异常

c++ - 如何将临时 C 数组传递到 constexpr 容器中

c++ - 什么是 boost system error_code number 2

javascript - 服务器代码中无法识别 Meteor 模板

C++ 模板元编程 : Inheritance from template template parameter

c++ - CTAD 无法在部分特化中使用 SFINAE 推导出模板参数