clang的c ++ 11支持可靠吗? [英] Is clang's c++11 support reliable?

查看:73
本文介绍了clang的c ++ 11支持可靠吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试混合clang(Apple LLVM 6.0版(clang-600.0.56)(基于LLVM 3.5svn,目标:x86_64-apple-darwin14.0.0),c ++ 11和CGAL)时,我遇到了一个有趣的问题(通过MacPorts)。

I ran into an interesting issue when trying to mix clang (Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn, Target: x86_64-apple-darwin14.0.0), c++11 and CGAL (via MacPorts).

似乎我是否调用 std :: vector<> :: reserve 将确定我的程序是否可以编译。

It seems that whether or not I call std::vector<>::reserve will determine whether my program will even compile.

我已将问题缩小为一个最小的示例(与CGAL示例一样少):

I've trimmed down the problem into a minimal example (as minimal as CGAL examples get):

#include <vector>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/AABB_triangle_primitive.h>

// CGAL::Epeck works fine, suggesting the problem is in CGAL::Epick
typedef CGAL::Epick Kernel;
typedef CGAL::Triangle_3<Kernel> Triangle_3; 
typedef typename std::vector<Triangle_3>::iterator Iterator;
typedef CGAL::AABB_triangle_primitive<Kernel, Iterator> Primitive;
typedef CGAL::AABB_traits<Kernel, Primitive> AABB_triangle_traits;
typedef CGAL::AABB_tree<AABB_triangle_traits> Tree;
typedef typename Tree::Point_and_primitive_id Point_and_primitive_id;
typedef CGAL::Point_3<Kernel>    Point_3;

template <typename BKernel>
void A()
{
  const CGAL::AABB_tree<
    CGAL::AABB_traits<BKernel, 
      CGAL::AABB_triangle_primitive<BKernel, 
        typename std::vector<CGAL::Triangle_3<BKernel> >::iterator
      >
    >
  > tree;
  Point_and_primitive_id pp = tree.closest_point_and_primitive(Point_3());
}

void B()
{
  std::vector<Triangle_3> T;
#ifdef MAGIC
  T.reserve(0);
#endif
  return A<Kernel>();
}

正在发出:

clang++ -std=c++11 -c example.cpp -I/opt/local/include

无法编译。出现以下错误:

This fails to compile. Giving errors like:

    In file included from example.cpp:1:
    In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:265:
    In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__bit_reference:15:
    In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:626:
    In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:157:
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tuple:228:60: error: 
          no member named 'value' in 'std::__1::is_convertible<const CGAL::Point_3<CGAL::Epick> &,
          CGAL::Point_3<CGAL::Epick> >'
                                   is_convertible<_Tp0, _Up0>::value &&
                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tuple:242:14: note: 
          in instantiation of template class 'std::__1::__tuple_convertible_imp<true, std::__1::__tuple_types<const
          CGAL::Point_3<CGAL::Epick> &, const CGAL::Vector_3<CGAL::Epick> &>,
          std::__1::__tuple_types<CGAL::Point_3<CGAL::Epick>, CGAL::Vector_3<CGAL::Epick> > >' requested here
        : public __tuple_convertible_imp<tuple_size<typename remove_reference<_Tp>::type>::value ==

但是,如果我对 std :: vector :: reserve 进行此魔术调用,则此会编译,发出:

However, this does compile if I make this magic call to std::vector::reserve, issuing:

clang++ -std=c++11 -c example.cpp -I/opt/local/include -DMAGIC

或通过禁用c ++ 11

or by disabling c++11

clang++ -c example.cpp -I/opt/local/include




  1. 这是CGAL或clang中的错误吗?

  2. 对于这种不稳定的编译器行为,有什么解释?

  3. 是有一种 clean 避免这种情况的方法(希望不会真正更改模板或函数原型的设置,因为我需要适合大型项目的解决方案)。

  1. Is this a bug in CGAL or clang?
  2. What explanation can there be for such erratic compiler behavior?
  3. Is there a clean way of avoiding this (hopefully without really changing the templating or function prototype set up as I need the solution to fit my larger project).


推荐答案

由于Apple的GCC已过时(2007年的最新GPL v2版本,GCC 4.2.1)而不是C ++ 11功能齐全(因此提供了libstdc ++),则可以通过MacPorts安装更现代的GCC版本( sudo端口安装gcc48 sudo端口安装gcc49 ),这将为您提供libstdc ++的更现代版本。我用以下代码测试了您的代码:

Since Apple's GCC is outdated (latest GPL v2 version from 2007, GCC 4.2.1) and not C++11 feature complete (hence the libstdc++ provided with it), you can install a more modern version of GCC through MacPorts (sudo port install gcc48 or sudo port install gcc49) and that will provide you a more modern version of libstdc++. I tested your code with:

/opt/local/bin/g++-mp-4.8 -std=c++11 -c example.cpp -I/opt/local/include

并成功编译。

如果您喜欢此解决方案并希望进行更简洁的编译器调用;您可以通过以下命令使用 gcc_select 将MacPorts的GCC设置为默认值(在我的情况下为gcc48):

If you prefer this solution and want a cleaner compiler call; you can set MacPorts' GCC as the default using gcc_select with the command (in my case for gcc48):

sudo port select --set gcc mp-gcc48

仅一次。然后,您可以使用

only once. Then, you can compile it with just

g++ -std=c++11 -c example.cpp -I/opt/local/include

在新的终端会话中。

这篇关于clang的c ++ 11支持可靠吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆