- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这两个非可变函数模板编译:
template <typename T, typename U>
typename std::enable_if<std::is_same<U, int>::value, void>::
type testFunction(T a, U b) {
std::cout << "b is integer\n";
}
template <typename T, typename U>
typename std::enable_if<std::is_same<U, float>::value, void>::
type testFunction(T a, U b) {
std::cout << "b is float\n";
}
但是,类似的可变参数模板无法编译:
template <typename T, typename... U>
typename std::enable_if<std::is_same<U, int>::value, void>::
type testFunction(T a, U... bs) {
std::cout << "bs are integers\n";
}
template <typename T, typename... U>
typename std::enable_if<std::is_same<U, float>::value, void>::
type testFunction(T a, U... bs) {
std::cout << "bs are floats\n";
}
也许我正在尝试做一些无法完成的事情。我知道使用初始化列表可以实现类似的功能,但我想避免初始化列表参数所需的大括号。
最佳答案
是的。您可以使用 fold expression在 C++17 中:
template <typename T, typename... U>
typename std::enable_if<(std::is_same<U, float>::value && ...), void>::
type testFunction(T a, U... bs) {
std::cout << "bs are floats\n";
}
在 C++11 中,您可以重新实现 std::conjunction
:
template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
template<class B1, class... Bn>
struct conjunction<B1, Bn...>
: std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
template <typename T, typename... U>
typename std::enable_if<
std::conjunction_v<std::is_same<U, float>...>,
void
>::type testFunction(T a, U... bs) {
std::cout << "bs are floats\n";
}
关于c++ - 是否可以将 'enable_if' 和 'is_same' 与可变函数模板一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48764158/
我正在使用这样的模板别名: template ::value>::type> using vec2 = std::pair; template ::value>::type> using vec3 =
基本问题陈述 我正在学习 SFINAE。我尝试了一个非常简单的 enable_if : // 1: A foo() that accepts arguments that are derived fr
在language reference of std::enable_if at cppreference包括以下注释 Notes A common mistake is to declare two
我对 SFINAE 有基本的了解,我想我理解了很多关于如何 std::enable_if 的例子。利用它来选择函数模板特化,但我很难理解它如何用于类模板。 以下例子来自cppreference.com
这有什么问题? 我认为这应该在使用 enable if 时起作用??? 帮助?? 不应该排除第二个构造函数吗? #include #include #include template class
我试图在模板类中声明函数,以便函数声明依赖于模板类型参数。 template struct Block { static bool parse(int32_t index,
我正在尝试一种基于类模板参数来专门化成员函数的方法,而不必在类上使用 SFINAE(并导致代码重复或创建另一个类)。 由于两个模板参数不能是可选的,并且参数 enable_if 在指南中是不受欢迎的,
我遇到一个问题,未知代码正在使用试图在编译时和运行时取消引用类型的元模板。这意味着,它们遍历指针层次结构直到找到匹配器。 现在通常这很好用。但是当用户传递类似的东西时: typedef struct
我试图了解 std::enable_if 是如何工作的 inn 模板参数。 #include #include #include using namespace std; class Inter
有谁知道为什么下面的代码可以编译 static const size_t CONSTANT = /* ... */; template = 0 > res_type foo() { // ...
我有一个类定义为: template class AdjacencyList; 其中 V 和 E 分别是顶点和边值的类型。 我目前正在尝试在 AdjacencyList 中定义以下成员函数: std
我有以下代码无法在 VC2010 上编译: #include using namespace std; template typename enable_if::type foo() { retu
为什么编译器对 std::tuple 的访问者具有以下定义顺序很重要 namespace TupleVisit{ //This function SHOULD BE DEFINED S
我想使用 type_traits 是否通过 shared_ptr 重载。 struct A { A(int i) : x(i) {} int x; }; int main() {
作为一个实验,我试图使一个没有参数的 void 成员函数根据类模板参数改变行为: #include #include template class MyClass { public: void
我正在使用 enable_if 语句来删除考虑中的可能方法。 #include "gmpxx.h" #include template struct is_ring_field { }; temp
是否有任何“更干净”的方式(我的意思是减少重复代码)来编写以下内容? template class Test { struct Foo1 { int a; }; struct F
在C ++中,请考虑以下示例: template struct q; template struct q { q() { cout struct q {
我对 std::enable_if 很陌生,想知道如何使用它。我有一个模板类: template class foo { } 现在我只想在 a + b 等于 10 时实例化它。我可以使用 std::e
我最近遇到了一个有趣的 enable_if 用法版本,它用于有条件地启用具有更好可读性的函数,因为该函数的返回类型不是 enable_if 的一部分(请参阅 cleaner_usage): #incl
我是一名优秀的程序员,十分优秀!