【问题标题】:C++14 variadic template argument inference in clang and gccclang 和 gcc 中的 C++14 可变参数模板参数推断
【发布时间】:2015-08-18 02:32:43
【问题描述】:

我正在使用 clang 3.5.0 和 gcc 版本 4.9.2(都启用了 C++14 选项,尽管可以在 C++11 中完成尾随返回类型)。

以下代码在 g++ 中编译,而不是在 clang++ 中。我的问题是“哪个是正确的?”

#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 报告

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) {
     ^

【问题讨论】:

  • 这是 Clang 中的一个错误,fixed with Clang 3.6
  • 啊..谢谢。我的 search-foo 不够好,无法找到这个。
  • 检查最新版本的编译器总是一个好主意。 melpon.org/wandbox 非常适合这个目的。

标签: c++ c++11 gcc c++14 clang++


【解决方案1】:

您应该了解,Clang 仍处于试验阶段(尽管它被“广泛”使用),并且基本上是基于 GCC 构建的(尽管它有一些重大变化),并且您可以确定一切(谈论 C++ 代码)是在 GCC 中编译应该在 Clang 中编译,如果不是,它是一个 Clang 错误。

GCC 是时候证明自己的强大了。

【讨论】:

  • 引用clang.llvm.org/cxx_status.html: Clang fully implements all published ISO C++ standards including C++11, as well as the upcoming C++14 standard, and some parts of the fledgling C++1z standard, and is considered a production-quality C++ compiler
  • 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. 所以不是真的。查看 g++ 和 clang 关于 c++ 语言中极端情况的所有错误报告。
  • 你喝了什么? “建立在 gcc 之上”这一点特别有趣——Clang 项目的重点是完全从头构建一个新的 C++ 前端,因为 gcc 的体系结构显示出其严重的局限性。此外,Clang 通常使用 LLVM 作为后端,因此甚至后端都不是通用的。
  • @Matteo Italia:你可能会误会我的意思。 “built over gcc”的含义代表 -> clang 旨在与 GCC 兼容并最终取代它(在意识形态上)。
  • @bolov:具有讽刺意味的是,您在一个问题页面中确认了clang错误。
猜你喜欢
  • 2016-10-05
  • 2020-10-27
  • 1970-01-01
  • 2012-11-11
  • 2013-07-22
  • 2022-12-02
  • 2018-05-16
  • 1970-01-01
  • 2013-09-14
相关资源
最近更新 更多