简体   繁体   English

g ++和clang ++不同的行为推断函数的模板返回类型

[英]g++ and clang++ different behaviour inferring the template return type of a function

Another "who's right between g++ and clang++?" 另一个“g ++和clang ++之间谁是正确的?” question for C++ standard gurus. C ++标准大师的问题。

The following program 以下程序

#include <iostream>

void foo (int v)
 { std::cout << "foo(), int version (" << v << ')' << std::endl; }

void foo (double v)
 { std::cout << "foo(), double version (" << v << ')' << std::endl; }

template <typename T, typename R>
void bar (T v, R(*fn)(T))
 { fn(v); }

int main ()
 { bar(1, foo); }

compile and run with g++ (6.3.0, but also with 8.0.0 according Wandbox) but compiling it with clang++ (3.9.1, but also with 6.0.0 according Wandbox) I get the following error 使用g ++编译和运行(6.3.0,但根据Wandbox也使用8.0.0)但是用clang ++编译它(3.9.1,但根据Wandbox也是6.0.0)我得到以下错误

tmp_002-11,14,gcc,clang.cpp:29:4: error: no matching function for call to 'bar'
 { bar(1, foo); }
   ^~~
tmp_002-11,14,gcc,clang.cpp:25:6: note: candidate template ignored: couldn't
      infer template argument 'R'
void bar (T v, R(*fn)(T))
     ^
1 error generated.

As usual the question is: who's right? 像往常一样,问题是:谁是对的? g++ or clang++? g ++或clang ++?

Clang is correct, but for a subtle reason: an overload set is allowed during template deduction, but deduction on that one argument must be able to select one of them for anything to be deduced. Clang是正确的,但是由于一个微妙的原因:在模板推导期间允许重载集,但是对该一个参数的推论必须能够选择其中一个来推导出任何东西。 (If zero match, the overall deduction fails; if more than one does, deduction ignores that argument.) Here, both foo s match R(*fn)(T) , so R cannot be deduced from that argument (even though they would both agree on it), and thus not at all. (如果零匹配,则整体推论失败;如果多于一个,则推论忽略该参数。)这里,两个foo匹配R(*fn)(T) ,因此R不能从该参数推导出来(即使它们会双方都同意,因此根本没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 g ++和clang ++使用指向可变参数模板函数的指针的不同行为 - g++ and clang++ different behaviour with pointer to variadic template functions g ++和clang ++使用自动参数的模板特化的不同行为 - g++ and clang++ different behaviour with template specialization for auto argument 带有集成模板参数的g ++和clang ++不同的行为 - g++ and clang++ different behaviour with integral template parameter g ++和clang ++使用变量模板和SFINAE的不同行为 - g++ and clang++ different behaviour with variable template and SFINAE g ++和clang ++在模板类中定义的朋友模板函数的不同行为 - g++ and clang++ different behaviour with friend template function defined inside a template class g ++和clang ++不同的行为推导可变参数模板`auto`值 - g++ and clang++ different behaviour deducing variadic template `auto` values g ++和clang ++使用运算符&lt;()重载的不同行为 - g++ and clang++ different behaviour with operator<() overloading g++ 和 clang++ 与可变参数容器的不同行为 - g++ and clang++ different behaviour with variadic container g ++和clang ++使用静态成员的递归初始化的不同行为 - g++ and clang++ different behaviour with recursive initialization of a static member g ++和clang ++与流输入和无符号整数的不同行为 - g++ and clang++ different behaviour with stream input and unsigned integer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM