简体   繁体   English

Clang和gcc中的C ++ 14可变参数模板参数推断

[英]C++14 variadic template argument inference in clang and gcc

I am using clang 3.5.0 and gcc version 4.9.2 (both with C++14 options on, although with trailing return types this could be done in C++11). 我正在使用clang 3.5.0和gcc版本4.9.2(都启用了C ++ 14选项,尽管使用尾随返回类型可以在C ++ 11中完成)。

The following code compiles in g++ and not in clang++. 以下代码在g ++中而不是在clang ++中编译。 My question is "which is right?" 我的问题是“哪个是对的?”

#include <iostream>
#include <tuple>
#include <functional>
using namespace std;

template<typename OP, typename F1, typename... Fs>
struct symop {
     OP op;
     tuple<F1,Fs...> fs;

     symop(const OP &oopp, const F1 &f1, const Fs &...ffss)
          : op(oopp), fs(f1,ffss...) {}
};

template<typename OP, typename... Fs>
auto baz(const symop<OP,Fs...> &so) {
     return so.op(get<0>(so.fs),get<1>(so.fs));
}

/* // this version compiles on both compilers
template<typename OP, typename F1, typename... Fs>
auto baz(const symop<OP,F1,Fs...> &so) {
     return so.op(get<0>(so.fs),get<1>(so.fs));
}
*/

int main() {
     symop<plus<int>,int,int> so{plus<int>{},3,4};
     cout << baz(so) << endl;
}

Clang reports lang报告

q.cpp:29:10: error: no matching function for call to 'baz'
        cout << baz(so) << endl;
                ^~~
q.cpp:16:6: note: candidate template ignored: couldn't infer template argument
      'OP'
auto baz(const symop<OP,Fs...> &so) {
     ^

You should understand that Clang is still experimental (even though it is "widely" used), and basically is built upon GCC (trough it has some significant changes) and you can be sure that everything (talking C++ code) that is compiled in GCC should compile in Clang, and if not, it is a Clang bug. 您应该了解Clang仍处于试验阶段(即使已“广泛”使用),并且基本上是基于GCC构建的(尽管它有一些重大更改),并且您可以确定在GCC中编译的所有内容(使用C ++代码)应该使用Clang编译,否则,这是Clang错误。

GCC had it's time to prove being robust. GCC有时间证明它很强大。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM