首页 > TAG信息列表 > function-templates

c – 为什么`const T&`不确定是const?

template<typename T> void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template<typename T> void g(T n) { f<T>(n, n); } int main() { int n{}; g<int&>(n); } 请注意:b是const T&和B是好的! 为什么const T&am

将不再允许在C 20中为程序定义类型的std中的函数模板进行专门化吗?

引自cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined type and the s

c – 如何使用给定的类名获取未知类的对象

我正在寻找一种方法来确定在运行时应该分配哪种类型的对象(基于给定的类名,类型为const char *). 那么最简单的方法当然是使用ifs / else ifs的负载,但这似乎不适用,因为我有> 100个不同的类(至少它们都来自一个基类),我必须定期添加新类. 我已经想出了初稿,但遗憾的是它还没有编译

功能模板c的显式专业化

template <typename T> bool validate(const T& minimum, const T& maximum, const T& testValue) { return testValue >= minimum && testValue <= maximum; } template <> bool validate<const char&>( const ch

c – 功能模板的规范

我想创建一个函数模板,其中类T仅限于特殊基类T_base的派生类.实现这一目标的有效方法是什么?谢谢你的帮助!解决方法:您可以使用类型特征和SFINAE template<typename T, bool[std::is_base_of<T_base, T>::value] = nullptr> void f(T const&); C 03版本也适用于C 11 tem