我已经派生了一个异常类std::runtime_error,以便添加对异常流的支持.我得到一个奇怪的编译器错误输出与clang,我不知道如何解决?
clang++ -std=c++11 -stdlib=libc++ -g -Wall -I../ -I/usr/local/include Main.cpp -c
Main.cpp:43:19: error: call to deleted constructor of 'EarthException'
throw EarthException(__FILE__, __LINE__)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../EarthException.hpp:9:12: note: function has been explicitly marked deleted here
struct EarthException : public Exception<EarthException>
template <typename TDerived>
class Exception : public std::runtime_error
{
public:
Exception() : std::runtime_error("") {}
Exception(const std::string& file, const unsigned line)
: std::runtime_error("")
{
stream_ << (file.empty() ? "UNNAMED_FILE" : file) << "[" << line << "]: ";
}
virtual ~Exception() {}
template …Run Code Online (Sandbox Code Playgroud) 我正在使用clang编译ac库(由c ++项目使用).在尝试链接此库时,我收到了链接器错误(特别是关于hqxInit函数的未定义符号).当我切换到clang ++时,它可以工作.用nm检查,clang ++进一步说明这些名字.发生了什么 - 有没有更好的方法告诉链接器一个库是munged-for-c而不是munged-for-c ++?用c ++构建ac库似乎很愚蠢....
//用clang构建
$ nm libhqx.a
libhqx.bak(init.c.o)
04000000 C _RGBtoYUV
00000004 C _YUV1
00000004 C _YUV2
00000000 T _hqxInit
Run Code Online (Sandbox Code Playgroud)
//用clang ++构建
$ nm libhqx.a
libhqx.a(init.o):
00000100 S _RGBtoYUV
04000100 S _YUV1
04000104 S _YUV2
00000000 T __Z7hqxInitv
Run Code Online (Sandbox Code Playgroud) 我试过谷歌搜索这个,但我得到的是如何编译基本程序的结果.是否可以使用Clang ++和G ++将代码编译为特定的C++标准,指定代码应该编译成C89,C99,C++ 98等?
Clang接受此代码,但GCC拒绝它:
class Foo {
public:
static constexpr double kVal = 0.25f;
};
const double Foo::kVal;
Run Code Online (Sandbox Code Playgroud)
(使用clang 3.0和g ++ 4.6.3)
~$ clang++ foo.cc -std=c++11 -c
[ok]
~$ g++ foo.cc -std=c++0x -c
foo.cc:6:19: error: redeclaration ‘Foo::kVal’ differs in ‘constexpr’
foo.cc:3:34: error: from previous declaration ‘Foo::kVal’
foo.cc:6:19: error: declaration of ‘constexpr const double Foo::kVal’ outside of class is not definition [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
哪种解释是正确的?
我有一些代码我维护我已经开始在clang 3.3下编译.使用"-std = c ++ 11"进行编译时,clang会生成错误(如下所示).我已经将违规代码提炼为以下内容:
#include <stdio.h>
#define DBG_PRT(__format, ...) \
printf("%s:%d:%s: "__format, __FILE__, \
__LINE__, __FUNCTION__, ## __VA_ARGS__)
int main()
{
DBG_PRT("%s\n", "Hi");
}
Run Code Online (Sandbox Code Playgroud)
这是铿锵的输出:
test.cpp:10:5:错误:没有匹配的文字运算符用于调用'operator'"__ format",参数类型为'const char*'和'unsigned int'
Run Code Online (Sandbox Code Playgroud)DBG_PRT("%s\n", "Hi"); ^ test.cpp:4:29: note: expanded from macro 'DBG_PRT' printf("%s:%d:%s: "__format, __FILE__, \ ^ 1 error generated.
如果字符串文字和"__format"之间没有空格,那么预处理器似乎不应该能够扩展__format.但是,当没有指定-std = c ++ 11时,显然是这样.G ++ 4.4.7(有和没有-std = c ++ 0x)编译得很好.
编译器有错误吗?
我有一个很长的代码,我们在团队中使用了很长时间.但是几个星期以来,当它在我的机器上编译时,它有问题.代码是针对Intel Atom CPU的交叉编译,并在特定计算机上运行.
当它在我的计算机上编译时,与其他任何人不同,它会导致一个segmentation fault.分段错误来自if不应执行的块内部:
Settings *s = &Global::getSettings();
std::cout << "Pointer value before if : " << s << std::endl;
if(s != 0)
{
std::cout << "Pointer value after if : " << &Global::getSettings() << std::endl;
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
Global::getSettings() 如下:
.
.
.
private:
static __thread Settings* theSettings;
public:
static Settings& getSettings() {return *theSettings;}
.
.
.
__thread Settings* Global::theSettings = 0;
Run Code Online (Sandbox Code Playgroud)
在我的测试中,Global :: theSettings的值没有改变,等于零.上面代码片段的输出是这样的:
Pointer value before if : 0
Pointer value …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的代码 - 它大大简化了,但这个代码片段编译并展示了相同的行为:
template <typename TFunc>
float FloatSelect( const float in_value, TFunc&& Predicate) {
return std::forward<TFunc>(Predicate)(in_value) ? in_value : 0.0f;
};
void DisplayFloatSelect() {
const float value = FloatSelect(
-1.0f,
[] (const float value) { return value > 0.0f; }
);
std::cout << value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
随着-Wshadow使编译器发出如下警告(如看到这里):
12 : warning: declaration shadows a local variable [-Wshadow]
[] (const float value) { return value > 0.0f; }
^
10 : note: previous declaration is here
const float value …Run Code Online (Sandbox Code Playgroud) 我没有经验丰富的LLVM用户,但我正在尝试在Windows上编译Linux LLVM项目.该项目是GHDL.
因为Windows的现成LLVM安装程序没有llvm-config捆绑,所以我需要编译LLVM并从源代码中获得批处理.该项目需要LLVM 3.5.
首先,我下载llvm-3.5.2并clang-3.5.2使用CMake将其转换为Visual Studio 2013项目.然后我用VS2013编译它.
原始的makefile调用llvm-config.生成的字符串将传递给clang++:
clang++ -c -I`/usr/lib/llvm-3.5/bin/llvm-config --includedir --cflags --cxxflags` -o llvm-cbindings.o src/ortho/llvm/llvm-cbindings.cpp
Run Code Online (Sandbox Code Playgroud)
我正在使用PowerShell调用llvm-config并将结果存储在变量中:
$LLVM_CONFIG_RESULT = & $LLVM_CONFIG --cxxflags
Run Code Online (Sandbox Code Playgroud)
结果是:
-IC:\Tools\LLVM-3.5/include /DWIN32 /D_WINDOWS /W3 /MP -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -wd4146 -wd4180 -wd4244 -wd4267 -wd4291 -wd4345 -wd4351 -wd4355 -wd4503 -wd4624 -wd4722 -wd4800 -w14062 -we4238 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
Run Code Online (Sandbox Code Playgroud)
我注意到几个问题:
/而不是\-IC:\Tools\LLVM-3.5/include-其他开关和参数一起使用/... -IC:\Tools\LLVM-3.5/include /DWIN32 /D_WINDOWS ...我试图将一些简单的计算向量化,以加快SIMD架构的速度.但是,我还想将它们作为内联函数,因为函数调用和非向量化代码也需要计算时间.但是,我不能总是同时实现它们.实际上,我的大多数内联函数都无法自动向量化.这是一个简单的测试代码:
inline void add1(double *v, int Length) {
for(int i=0; i < Length; i++) v[i] += 1;
}
void call_add1(double v[], int L) {
add1(v, L);
}
int main(){return 0;}
Run Code Online (Sandbox Code Playgroud)
在Mac OS X 10.12.3上,编译它:
clang++ -O3 -Rpass=loop-vectorize -Rpass-analysis=loop-vectorize -std=c++11 -ffast-math test.cpp
test.cpp:2:5: remark: vectorized loop (vectorization width: 2, interleaved count: 2) [-Rpass=loop-vectorize]
for(int i=0; i < Length; i++) v[i] += 1;
^
Run Code Online (Sandbox Code Playgroud)
但是,非常相似的东西(只在call_add1中移动参数)不起作用:
inline void add1(double *v, int Length) {
for(int i=0; i < Length; i++) v[i] += 1;
} …Run Code Online (Sandbox Code Playgroud) 我遇到了代码无法为我正在使用的外部库编译的问题.我相信库用gcc编译得很好,但它无法用clang为我编译.
我可以重新创建如下问题
template <class T>
class A {
public:
struct B {
int a;
};
void test();
private:
T _t;
};
template <class T>
void A<T>::test()
{
printf("Result %d", std::numeric_limits<decltype(B::a)>::max());
}
int main(int argc, char** argv)
{
auto t = A<int>();
t.test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这无法在clang上编译时出现以下错误
error: invalid use of non-static data member 'a' printf("Result %d", std::numeric_limits<decltype(B::a)>::max());
Run Code Online (Sandbox Code Playgroud)
我的问题如下:
预期的行为是什么?
在c ++ 11中添加了非静态成员的decltype.这适用于模板类中声明的那些吗?
这是编译器错误吗?或者使用gcc的不符合代码的示例?