// #1
template <typename T, T n1, T n2>
struct foo {
static const char* scenario() {
return "#1 the base template";
}
};
// #2
// partial specialization where T is unknown and n1 == n2
template <typename T, T a>
struct foo<T, a, a> {
static const char* scenario() {
return "#2 partial specialization";
}
};
下面的主要在 g++ (6.1) 上得到不同的结果和 clang++ (3.8.0) :
extern const char HELLO[] = "hello";
double d = 2.3;
int main() {
cout << foo<int, 1, 2> ::scenario() << endl;
cout << foo<int, 2, 2> ::scenario() << endl;
cout << foo<long, 3, 3> ::scenario() << endl;
cout << foo<double&, d, d> ::scenario() << endl;
cout << foo<double*, &d, &d> ::scenario() << endl;
cout << foo<double*, nullptr, nullptr> ::scenario() << endl;
cout << foo<int*, nullptr, nullptr> ::scenario() << endl;
cout << foo<nullptr_t, nullptr, nullptr> ::scenario() << endl;
cout << foo<const char*, HELLO, HELLO> ::scenario() << endl;
}
g++ 上的结果和 clang++ <b>#</b> | <b>The code</b> | <b>g++ (6.1)</b> | <b>clang++ (3.8.0)</b> |
1 | foo<int, 1, 2> | #1 as expected | #1 as expected |
2 | foo<int, 2, 2> | #2 as expected | #2 as expected |
3 | foo<long, 3, 3> | #2 as expected | #2 as expected |
4 | foo<double&, d, d> | #1 -- why? | #2 as expected |
5 | foo<double*, &d, &d> | #2 as expected | #2 as expected |
6 | foo<double*, nullptr, nullptr> | #2 as expected | #1 -- why? |
7 | foo<int*, nullptr, nullptr> | #2 as expected | #1 -- why? |
8 | foo<nullptr_t, nullptr, nullptr> | #2 as expected | #1 -- why? |
9 | foo<const char*, HELLO, HELLO> | #2 as expected | #2 as expected |
代码:https://godbolt.org/z/4GfYqxKn3
编辑,2021 年 12 月:
自原始帖子以来的几年里,结果发生了变化,and were even identical for gcc and clang at a certain point in time ,但再次检查,g++ (11.2)和 clang++ (12.0.1) changed their results on references (case 4), but still differ on it .目前看来gcc一切正常,clang在引用案例上是错误的。
<b>#</b> | <b>The code</b> | <b>g++ (11.2)</b> | <b>clang++ (12.0.1)</b> |
1 | foo<int, 1, 2> | #1 as expected | #1 as expected |
2 | foo<int, 2, 2> | #2 as expected | #2 as expected |
3 | foo<long, 3, 3> | #2 as expected | #2 as expected |
4 | foo<double&, d, d> | #2 as expected | #1 -- why? |
5 | foo<double*, &d, &d> | #2 as expected | #2 as expected |
6 | foo<double*, nullptr, nullptr> | #2 as expected | #2 as expected |
7 | foo<int*, nullptr, nullptr> | #2 as expected | #2 as expected |
8 | foo<nullptr_t, nullptr, nullptr> | #2 as expected | #2 as expected |
9 | foo<const char*, HELLO, HELLO> | #2 as expected | #2 as expected |
最佳答案
我将把我的答案奉献给案例 #4,因为根据 OP 的 EDIT,编译器现在就案例 #6-8 达成一致:
# | The code | g++ (6.1) | clang++ (3.8.0) |
4 | foo<double&, d, d> | #1 -- why? | #2 as expected |
好像是 clang++ 3.8.0行为正确,gcc 6.1由于以下错误已在 gcc 7.2 中修复,因此拒绝此案例的完美偏特化:
Bug 77435 - Dependent reference non-type template parameter not matched for partial specialization
有一个diff编译器代码中有这个关键变化:
// Was: else if (same_type_p (TREE_TYPE (arg), tparm))
else if (same_type_p (non_reference (TREE_TYPE (arg)), non_reference(tparm)))
在 gcc 7.2 之前, 当依赖类型 T&与 T 类型的参数匹配在部分特化候选中,编译器错误地拒绝了它。这种行为可以用一个更简洁的例子来说明:
template <typename T, T... x>
struct foo {
static void scenario() { cout << "#1" << endl; }
};
// Partial specialization when sizeof...(x) == 1
template <typename T, T a>
struct foo<T, a> {
static void scenario() { cout << "#2" << endl; }
};
在 T = const int 的情况下gcc 6.1 的行为和 gcc 7.2是一样的:
const int i1 = 1, i2 = 2;
foo<const int, i1, i2>::scenario(); // Both print #1
foo<const int, i1>::scenario(); // Both print #2
但是如果是 T = const int& gcc 6.1 的行为是拒绝正确的部分特化并选择基础实现:
foo<const int&, i1, i2>::scenario(); // Both print #1
foo<const int&, i1>::scenario(); // gcc 6.1 prints #1 but gcc 7.2 prints #2
它影响任何引用类型,这里有更多示例:
double d1 = 2.3, d2 = 4.6;
struct bar {};
bar b1, b2;
foo<double&, d1, d2>::scenario(); // Both print #1
foo<double&, d1>::scenario(); // gcc 6.1 prints #1 but gcc 7.2 prints #2
foo<bar&, b1, b2>::scenario(); // Both print #1
foo<bar&, b1>::scenario(); // gcc 6.1 prints #1 but gcc 7.2 prints #2
您可以在此处运行此示例:https://godbolt.org/z/Y1KjazrMP
gcc似乎在 gcc 7.1 之前犯了这个错误但来自 gcc 7.2由于上面的错误修复,直到当前版本它正确地选择了部分特化。
总之,案例#4 在问题中的结果只是一个更普遍问题的症状,它的发生只是因为 double&是引用类型。为了证明这一说法,请尝试在 OP 的代码中添加以下行(以及我的示例中的 bar 、 b1 定义):
cout << foo<bar&, b1, b1>::scenario() << endl;
并观察 gcc 6.1再次打印 "#1 the base template"而gcc 7.2及以后打印"#2 partial specialization"正如预期的那样。
关于 OP 编辑中的后续问题:
# | The code | g++ (11.2) | clang++ (12.0.1) |
4 | foo<double&, d, d> | #2 as expected | #1 -- why? |
我认为 g++ (11.2)是正确的。
注意clang没有完全翻转它的答案,因为在您的链接中,您使用了 c++20标准,但如果你把它改回 c++14和原来的问题一样,甚至 clang++ 12.0.1同意g++ 11.2并选择偏专精。
实际上,它发生在 clang在 c++17也是,这似乎是 clang 中的一个问题从这个标准开始,直到今天才修复。
如果您尝试将以下测试用例添加到您的代码中:
TEST (foo<const int, 2, 2>); // clang (c++17/20) prints #1 and gcc (any) prints #2
clang也选择基本模板而不是像 gcc 这样的偏特化。而在这个测试用例中:
TEST (foo<int, 2, 2>); // Both agree on #2
他们都同意,我觉得这很奇怪,因为这添加了 const类型不应该影响部分特化的适应度,看起来像 clang不仅对引用这样做,对 const 也是如此!并且仅当标准 >= C++17 时。
顺便说一句,这个问题也可以在我的示例中重现: https://godbolt.org/z/W9q83j3Pq
注意 clang 8.0.0仅通过更改语言标准就与自己不同意,并且一直这样做直到 clang 13.0.0即使在不需要参数值相等的这种简单情况下。
clang 中这些奇怪的模板推论提出足够的“危险信号”,所以我必须得出结论 g++ (11.2)是正确的。
我的猜测是 - 引入了 C++17 CTAD , 这使得 clang与类模板推导不同,这个问题在某种程度上与其新实现有关,而旧的 C++14 实现保持不变。
关于c++ - 整型非类型参数和非整型非类型的模板偏特化,g++和clang的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369129/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
请帮助我理解范围运算符...和..之间的区别,作为Ruby中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano