- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试创建一个示例类,它根据其模板参数是否可以用某些参数构造而具有不同的特化。在我的示例中,使用了一个简单的 int
。我试过:
template<class A_t>
struct creator
{
A_t* ptr = nullptr;
};
template<class A_t>
struct creator<decltype(A_t(int()))>
{
A_t* ptr = new A_t(5);
};
struct A
{
int i;
A(int i) : i(i) {}
};
int main() {
std::cout << creator<A>().ptr << std::endl;
return 0;
}
我的意图是它打印一个自动构造的对象的内存地址。但是,它打印的是 0。因此,它采用的是非专用模板。
A_t
可以用该语法推导出来,因为 A_t
是明确给出的。此外,decltype(A_t(int())
的类型是A_t
(例如不是A_t&&
),一个简单的测试:
std::cout << std::is_same<decltype(A(int()), A>::value << std::endl;
打印 1。
但是,该实现有效:
#include <iostream>
template<class A_t, class = A_t>
struct creator
{
A_t* ptr = nullptr;
};
template<class A_t>
struct creator<A_t, decltype(A_t(int()))>
{
A_t* ptr = new A_t(5);
};
struct A
{
int i;
A(int i) : i(i) {}
};
int main() {
std::cout << creator<A>().ptr << std::endl;
return 0;
}
Coliru用两个类进行测试,一个接受 int
作为参数,另一个不接受。
为什么第一种方法不起作用?
最佳答案
我对你问题背后的想法最好的解释是:
对于给定的实例化类型 U
=> A_t
, 如果转换 U(int)
未定义则替换 U(int)
=> A_t(int)
将在上下文中失败 decltype(A_t(int()))
和专业:
template<class A_t>
struct creator<decltype(A_t(int()))>
{
A_t* ptr = new A_t(5);
};
将被删除,只留下基础模板实例化。但如果转换 U(int)
定义为替换将成功。
那么,因为:
std::is_same<U,decltype(U(int()))>::value == true
两位候选人:
// Tweedledum, with of U => A_t
struct creator<U>
{
U* ptr = nullptr;
};
和:
// Tweedledee, with U => decltype(A_t(int()))
struct creator<U>
{
U* ptr = new U(5);
};
正在运行中。
然后,将选择最专业的候选人,这将是Tweedledee
,因为满足U
=> decltype(A_t(int()))
约束 U
比裸体U => A_t
.
这个推理隐含地依赖于它对于 Teedledee
是可推导的那U
= decltype(A_t(int()))
什么时候U
=> A_t
;归结为可以推断出 U(int)
= A_t(int)
在唯一的模板参数中 decltype(A_t(int()))
.
你相信是这样然后想知道 gcc 如何选择 Tweedledum
.
正如@WhozCraig 所指出的,clang++ 明确拒绝了您的可推导性前提:-
$ clang++-3.8 -Wall -Wextra -pedantic -std=c++14 main.cpp
main.cpp:10:8: warning: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used
struct creator<decltype(A_t(int()))>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:9:16: note: non-deducible template parameter 'A_t'
template<class A_t>
^
事实证明,gcc 在这里被引入了一个诊断错误你的 SFINAE 措辞不吉利,decltype(A_t(int()))
如果将其替换为 decltype(A_t{int()})
, 然后 gcc 也是 gets with the program :
$ g++-6 -Wall -Wextra -pedantic -std=c++14 main.cpp
main.cpp:10:8: error: template parameters not deducible in partial specialization:
struct creator<decltype(A_t{int()})>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:10:8: note: ‘A_t’
在那之后,clang++ 是三个编译器中唯一的编译器完全编译程序 - 并实例化 creator<A>
来自基本模板。
最终的共识是A_t
在 decltype(A_t{int()})
中不可推导得到 C++14 标准的认可:
Deducing template arguments from a type [temp.deduct.type]
1 Template arguments can be deduced in several different contexts, but in each casea type that is specified in terms of template parameters (call it P) is comparedwith an actual type (call it A), and an attempt is made to find template argumentvalues (a type for a type parameter, a value for a non-type parameter, or atemplate for a template parameter) that will make P, after substitution of thededuced values (call it the deduced A), compatible with A.
...
4 In most cases, the types, templates, and non-type values that are used to composeP participate in template argument deduction... In certain contexts, however,the value does not participate in type deduction, but instead uses the valuesof template arguments that were either deduced elsewhere or explicitly specified.If a template parameter is used only in non-deduced contexts and is not explicitlyspecified, template argument deduction fails.
5 The non-deduced contexts are:
...
(5.2) - The expression of a decltype-specifier.
...
第 4 段还解释了为什么您的第二种方法会成功。
关于c++ - 构造函数参数的 SFINAE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41027701/
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
我是一名优秀的程序员,十分优秀!