标签: clang++

为什么这段代码在Clang ++中有效但不在G ++中?

请考虑以下代码:

struct Foo
{
    int x, y;

    Foo() = default;
    Foo(const Foo&) = delete;
    Foo& operator=(const Foo&) = delete;
};

int main()
{
    Foo f1 {1, 2};
    Foo f2 = {1, 2};
}
Run Code Online (Sandbox Code Playgroud)

使用clang ++进行编译不会产生任何错误:

$ clang++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
$ clang++ -std=c++11 -stdlib=libc++ -pedantic t.cpp -o out
...builds and runs fine...
Run Code Online (Sandbox Code Playgroud)

但是,通过ideone使用g ++ 4.8.1进行编译会产生错误:

prog.cpp: In function ‘int main()’:
prog.cpp:12:17: error: no matching function for …
Run Code Online (Sandbox Code Playgroud)

c++ g++ c++11 clang++

10
推荐指数
1
解决办法
480
查看次数

在Mac OS X Yosemite 10.10.1上包含搜索路径

我只是想改变包含搜索路径顺序(我相信).


我想更改包含搜索路径.特别是,我/usr/local/include首先需要.

但由于重复,它不会改变.我该怎么改变它?

我想有默认设置,因为我没有指定的路径出现.喜欢/usr/include/c++/4.2.1,/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include

 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test_common.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241.9 -v -gdwarf-2 -coverage-file /Users/machidahiroaki/RubymineProjects/caffe/.build_debug/src/caffe/test/test_common.o -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -D DEBUG -D CPU_ONLY -I /usr/local/include -I /usr/local/include -I /Users/machidahiroaki/anaconda/include -I /Users/machidahiroaki/anaconda/include/python2.7 -I /Users/machidahiroaki/anaconda/lib/python2.7/site-packages/numpy/core/include -I .build_debug/src -I ./src -I ./include -I /usr/local/atlas/include -stdlib=libstdc++ -O0 -Wall -Wno-sign-compare -Wno-unneeded-internal-declaration -fdeprecated-macro -fdebug-compilation-dir /Users/machidahiroaki/RubymineProjects/caffe -ferror-limit 19 -fmessage-length 0 -pthread -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option …
Run Code Online (Sandbox Code Playgroud)

c++ macos xcode clang++ osx-yosemite

10
推荐指数
1
解决办法
4万
查看次数

clang 3.4 C++ 14支持

我正在使用travis.ci来完成我的git存储库的自动测试版本.

对于linux,他们使用:Ubuntu 12.04
with clang 3.4

根据clang页面,Clang 3.4支持所有C++ 14语言功能(只要你使用-std = c ++ 1y标志).

到目前为止一直这么好:
我还需要使用std::index_sequence<t0,...,tn>哪种库功能N3658不是语言功能.但是我找不到任何关于为clang更新C++标准库的具体文档,以确保支持此功能(它不支持开箱即用).

TestCode:

#include <utility>
int main() {
    std::index_sequence<1,2,3,4>    seq;
}
Run Code Online (Sandbox Code Playgroud)

TestBuild:

> clang++ -std=c++1y pl.cpp
pl.cpp:3:10: error: no member named 'index_sequence' in namespace 'std'
    std::index_sequence<1,2,3,4>    seq;
    ~~~~~^
pl.cpp:3:37: error: use of undeclared identifier 'seq'
    std::index_sequence<1,2,3,4>    seq;
                                    ^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)

更新:

基于下面的建议,我尝试使用libc ++.
我确实做错了什么,但我从未尝试过使用替代标准库,所以我不确定这里出了什么问题.将在今晚挖掘.但如果您有任何建议,请发表评论.

> sudo apt-get install -qq libc++1 libc6 libc++-dev

> clang++ -stdlib=libc++ pl.cpp
pl.cpp:1:10: fatal …
Run Code Online (Sandbox Code Playgroud)

c++ clang++ c++14

10
推荐指数
1
解决办法
1692
查看次数

运算符<< on template argument type member仅在clang中导致错误

我有这个例子:

#include <iostream>
#include <tuple>
#include <string>

template <typename T>
class A {
public:
    A(const T &t) : m_t(t) {}
    void foo() {
        std::cout << m_t << std::endl;
    }

private:
    T m_t;
};

typedef std::tuple<std::string, std::string> Type;
std::ostream &operator<<(std::ostream &os, const Type &t) {
    os << std::get<0>(t) << " " << std::get<1>(t);
    return os;
}

int main() {
    A<Type> a(Type{"ala", " ma kota"});
    a.foo();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

与clang ++(3.6)生成:

test_clang.cpp:10:19: error: call to function 'operator<<' that is neither visible in …
Run Code Online (Sandbox Code Playgroud)

c++ templates g++ language-lawyer clang++

10
推荐指数
1
解决办法
374
查看次数

转换操作符函数在g ++中编译得很好,但在其他编译器中却没有.为什么?

考虑以下计划:

struct S {
    using T = float;
    operator T() { return 9.9f; }
};
int main() {
    S m;
    S::T t = m;
    t = m.operator T(); // Is this correct ?
}
Run Code Online (Sandbox Code Playgroud)

该程序在g ++中编译良好(请参阅此处的实时演示)

但它在clang ++,MSVC++和Intel C++编译器的编译中失败了

clang ++给出以下错误(请参阅此处的在线演示)

main.cpp:8:20: error: unknown type name 'T'; did you mean 'S::T'?
    t = m.operator T(); // Is this correct ?
                   ^
                   S::T
main.cpp:2:11: note: 'S::T' declared here
    using T = float;
Run Code Online (Sandbox Code Playgroud)

MSVC++给出以下错误(请参阅此处的实时演示)

source_file.cpp(8): error …
Run Code Online (Sandbox Code Playgroud)

c++ g++ visual-c++ language-lawyer clang++

10
推荐指数
1
解决办法
261
查看次数

在Windows上使用clang编译CUDA时的重新定义

尽管已经提出了一个几乎完全相同的问题,但答案是针对OSX并且不再适用(反正真的是hacky).

问题是当在窗口上用clang编译cuda时,有大量的重新定义math_functions.hpp.

随着一些调查,显然CUDA决定把它math_functions.hppmath_functions.h功能namespace std(这是甚至合法的吗?),并在所有的libstdc ++函数相撞cmath和铛自己的编译CUDA头.

我该如何处理?最好不要使用上一个问题中显示的hacky方式?

边注

根据clang的文档,clang可以基于__global__/ __device__限定符重载,代码不应该只编译吗?

细节

版本:
铿锵4.0.0(建立像这样)
的libstdc ++从GCC 7.1.0来
CUDA 8.0
窗口10

完整的错误输出

clang++ hellocuda.cu  --cuda-path=E:\cuda\development --cuda-gpu-arch=sm_20 -LE:\cuda\development\lib\x64 -lcudart_static -ldl -lrt -std=c++1y -O2 -pedantic -Wall -Wextra -fms-extensions -o program.exe
In file included from <built-in>:1:
In file included from E:\LLVM\bin\..\lib\clang\4.0.0\include\__clang_cuda_runtime_wrapper.h:191:
E:\cuda\development/include\math_functions.hpp:1684:14: error: redefinition of
      '__isnan'
__func__(int __isnan(double a))
             ^
E:\TDM-GCC\mingw64 7.1.0\mingw64\x86_64-w64-mingw32\include\math.h:525:28: note:
      previous definition is here …
Run Code Online (Sandbox Code Playgroud)

c++ cuda clang++

10
推荐指数
1
解决办法
526
查看次数

使用函数类型语法声明成员函数

就在最近,我了解到你可以使用类似函数类型的变量语法来声明一个函数(包括方法):

using function_type = int (double);
// pre-C++11:
//typedef int function_type(double);

function_type fun_global;

struct methods
{
    static function_type mem_fun_static;
    function_type mem_fun_normal;
    virtual function_type mem_fun_virtual;
    virtual function_type mem_fun_abstract = 0;
};
Run Code Online (Sandbox Code Playgroud)

在上面的代码中

  • fun_global 是一个全球性的功能,
  • mem_fun_static是一个static成员函数,
  • mem_fun_normal 是一种普通的方法,
  • mem_fun_virtual是一种virtual方法,
  • mem_fun_abstract 是一种抽象的方法.

所有这些都采用单一的类型参数double和返回int值 - 就像function_type说的那样.

这些年来我都知道C++而且我不知道这个 - 这种语言永远不会让我感到惊讶!顺便说一下 - 这里提到的语法是什么?我没看到这个......

然而,在探索这个新功能时,我偶然发现编译器之间存在一些不一致之处.对于测试,我使用以下编译器:

  • GCC 5.4.0和7.1.0,命令行: g++ -Wall -Wextra -pedantic -std=c++14
  • Clang 4.0.1,命令行: clang++ -Wall -Wextra -pedantic -std=c++14
  • MSVC 19.10.25019(VS 2017),命令行: cl /W4 …

c++ g++ clang++ cl

10
推荐指数
1
解决办法
462
查看次数

使用自动时不检查数组范围

使用编译此代码-Warray-bounds。声明array2时收到警告array index 3 is past the end of the array (which contains 3 elements)。但是,在声明array1时,即使它必须是相同的类型,从而携带相同的大小信息,也并非如此。这是c中的错误吗?

enum class Format : int {
  Off = 55,
  FormatA = 66,
  FormatB = 77,
};

inline Format (&AllFormats())[3] {
  static Format values[] = {
    Format::Off,
    Format::FormatA,
    Format::FormatB
  };
  return values;
}

int main()
{
    auto array1 = AllFormats();    
    auto v3 = array1[3];

    Format (&array2)[3] = AllFormats();    
    v3 = array2[3];
}
Run Code Online (Sandbox Code Playgroud)

c++ clang clang++

10
推荐指数
1
解决办法
257
查看次数

Clang修改析构函数中的返回值?

在尝试编写一个类,调用它的构造函数和析构函数之间的持续时间时,我遇到了我认为是clang中的错误.(编辑:这不是一个bug;它是实现定义的副本省略)

timer下面的结构保存一个指向作为引用传入的持续时间对象的指针,并将范围的持续时间添加到此.

#include <iostream>
#include <chrono>
struct timer {
    using clock      = std::chrono::high_resolution_clock;
    using time_point = clock::time_point;
    using duration   = clock::duration;
    duration* d_;
    time_point start_;
    timer(duration &d) : d_(&d), start_(clock::now()) {}
    ~timer(){
        auto duration = clock::now() - start_;
        *d_ += duration;
        std::cerr << "duration: " << duration.count() << std::endl;
    }
};

timer::duration f(){
    timer::duration d{};
    timer _(d);
    std::cerr << "some heavy calculation here" << std::endl;
    return d;
}

int main(){
    std::cout << "function: " << f().count() << std::endl;
} …
Run Code Online (Sandbox Code Playgroud)

c++ clang++

9
推荐指数
1
解决办法
202
查看次数

clang++ 不会在 macOS Catalina 中编译 cmath

我有一个简单的 C++ 程序,可以导入cmath.

#include <cmath>

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下命令编译它。

clang++ -o test main.cpp -std=c++14
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误

In file included from main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: 
      no member named 'signbit' in the global namespace
using ::signbit;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:318:9: error: 
      no member named 'fpclassify' in the global namespace
using ::fpclassify;
      ~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:319:9: error: 
      no member named 'isfinite' in the global namespace; did you mean 'finite'?
using ::isfinite;
      ~~^
/usr/local/include/math.h:749:12: note: 'finite' declared here
extern int finite(double)
           ^
In file included …
Run Code Online (Sandbox Code Playgroud)

macos clang cmath clang++ macos-catalina

9
推荐指数
1
解决办法
1913
查看次数