- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
为什么下面的代码会出错?我认为编译器只是在这里选择适当的重载?
#include <iostream>
using std::cout;
using std::endl;
template <typename ToCheckFor>
struct InterfaceCheck {
// used by the constexpr function, the function will pass in a pointer to
// a type with the required types
template <typename _ToCheckFor, void (_ToCheckFor::*) ()>
struct InterfaceCheckImplTag {};
// used to check for the presence of a function print()
// template <typename T>
// static constexpr bool function(__attribute__((unused)) void* ptr) {}
template <typename T>
static constexpr bool function(__attribute__((unused)) void* ptr) {
return false;
}
template <typename T>
static constexpr bool function (__attribute__((unused))
InterfaceCheckImplTag<T, &T::print>* ptr) {
return true;
}
constexpr static const bool value = function<ToCheckFor>(nullptr);
};
struct Something {
void print() { cout << "Something::print()" << endl; }
};
int main() {
cout << InterfaceCheck<Something>::value << endl;
return 0;
}
为什么用省略号替换 void*
参数会使代码按预期工作?所以下面的代码按预期工作
#include <iostream>
using std::cout;
using std::endl;
template <typename ToCheckFor>
struct InterfaceCheck {
// used by the constexpr function, the function will pass in a pointer to
// a type with the required types
template <typename _ToCheckFor, void (_ToCheckFor::*) ()>
struct InterfaceCheckImplTag {};
// used to check for the presence of a function print()
// template <typename T>
// static constexpr bool function(__attribute__((unused)) void* ptr) {}
template <typename T>
static constexpr bool function(...) {
return false;
}
template <typename T>
static constexpr bool function (__attribute__((unused))
InterfaceCheckImplTag<T, &T::print>* ptr) {
return true;
}
constexpr static const bool value = function<ToCheckFor>(nullptr);
};
struct Something {
void print() { cout << "Something::print()" << endl; }
};
int main() {
cout << InterfaceCheck<Something>::value << endl;
return 0;
}
最佳答案
Why does the following code cause an error?
function
重载有两种可行的选择。两者都涉及从提供的参数进行的转换,并且两种转换都不比另一种更好:
error: call to 'function' is ambiguous
constexpr static const bool value = function<ToCheckFor>(nullptr);
^~~~~~~~~~~~~~~~~~~~
test.cpp:36:13: note: in instantiation of template class 'InterfaceCheck<Something>' requested here
cout << InterfaceCheck<Something>::value << endl;
^
test.cpp:17:27: note: candidate function [with T = Something]
static constexpr bool function(__attribute__((unused)) void* ptr) {
^
test.cpp:21:27: note: candidate function [with T = Something]
static constexpr bool function (__attribute__((unused))
用 function(...)
修复是有效的,因为从任何东西到 ...
的转换总是比其他任何东西“更差”的匹配(但仍然合法) .一旦你知道了,这是一个绝妙的技巧。
来自13.3.3.2 Ranking implicit conversion sequences [over.ics.rank]:
When comparing the basic forms of implicit conversion sequences (as defined in 13.3.3.1)
a standard conversion sequence (13.3.3.1.1) is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and
a user-defined conversion sequence (13.3.3.1.2) is a better conversion sequence than an ellipsis conversion sequence (13.3.3.1.3).
历史
我是从Modern C++ Design 开始学习这项技术的,第 2.7 节。我不确定这是不是它的发明地。但这不是一个糟糕的猜测。这本书现在已有 15 年历史,仍然值得一读。
关于c++ - SFINAE 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36268560/
C++11 专家的几个问题。 我正在与 SFINAE 打交道,我遇到了一个奇怪的情况,其中 g++ (4.9.2) 和 clang++ (3.5.0) 的行为不同。 我准备了以下示例代码。很抱歉,我无
这些天我正在试验 SFINAE,有些事情让我很困惑。为什么 my_type_a不能在 my_function 中推导出来的实例化? class my_type_a {}; template clas
我正在尝试复制(我猜)典型的 SFINAE 示例来判断一个类型是否具有特定方法。我的代码基本上是 the one found in the accepted answer of a previous
出于学术原因,我想实现一个示例,如果非类型模板参数满足给定条件,则选择一个模板。例如,我想要一个只为奇数定义的函数。 可以这样做: template struct is_odd: public st
有没有办法检查两个可变参数包的串联是否与第三个可变参数包相同。 template struct ClassOne { } template struct ClassTwo { } template s
为什么以下代码无法编译?为具有特定成员的类型启用模板的最简洁的解决方案是什么?如果模板变量被直接用于初始化它的表达式替换,它也会编译。 #include template constexpr boo
代码: #include using std::nullptr_t; template using nullptr_vt = nullptr_t; struct not_addable{}; tem
我有一个模板类,我想有两个复制 ctor。一个用于平凡类型,另一个用于非平凡类型。 以下代码有效(使用一个拷贝 ctor): template struct MyStruct { MySt
我一直在尝试定义一个辅助类来帮助我使用模板方法,在该方法中我希望为复杂类型和实际类型提供通用实现。 到目前为止,这是我的尝试: #include #include template struct is
这个问题已经有答案了: Officially, what is typename for? [duplicate] (8 个回答) 已关闭 3 年前。 我正在研究现代 C++ 中的 SFINAE,我看
我有一个 std::variants 包含具有不同接口(interface)的对象。目标是如果变体中的对象具有某些方法,则调用它。 我正在尝试制作几个模板装饰器,并寻找一种方法来使用更少的样板和没有宏
我有一个代码,它接受一个函数并根据函数签名执行它,如下所示: template struct Value { int value[Num]; }; struct Executor { t
我一直在尝试定义一个辅助类来帮助我使用模板方法,在该方法中我希望为复杂类型和实际类型提供通用实现。 到目前为止,这是我的尝试: #include #include template struct is
在此视频中https://youtu.be/Vkck4EU2lOU?t=582 “标签调度”和SFINAE作为替代方案出现,允许实现所需模板功能的选择。 正确吗? “标签发送”不是使用SFINAE吗?
我认为下面的代码会编译,因为冲突的重载是 SFINAEd 了。但是编译器(GCC)说:void Foo::bar(Xd) const' cannot be overloaded .有没有简单的方法来修
#define BINDINGTEMPLATE template, int> || std::is_same_v, std::string> || std::is_same_v, char>>> 这样
SFINAE 是否在概念论证中起作用? (也许这里不叫 SFINAE)。例子: template requires std::invocable && // , void>)
在他的演讲现代模板元编程:纲要第 I 部分中,Walter Brown 以他的方式讨论了 enable_if 与 SFINAE 的交互。 在大约 47:40 的谈话中,他被问到一个问题,我无法完全匹配
我正在尝试对同一函数进行两次重载,称为某物。这个函数应该以另一个函数作为参数,并且它应该根据这个另一个函数的返回类型进行重载。到目前为止我有这个: #include #include using
以下代码特化了 f() 的两个版本。第一个检测一个 vector 并返回一个迭代器。第二个接受所有其他类型并返回一个拷贝。 这无法在 VC 2010 上编译,GetIter2 中有一个错误,即 Get
我是一名优秀的程序员,十分优秀!