小编Big*_*awg的帖子

与g ++ 6.2不同的异常说明符

有人可以向我解释为什么这个代码不能用g ++版本6.2.0编译,但是用clang ++版本3.9.0-svn274438-1和icpc版本16.0.2编译得很好

$ cat wtf.cpp
#include <cstdio>
#include <new>
void *operator new(std::size_t) throw(std::bad_alloc);
void *operator new(std::size_t) throw (std::bad_alloc) { void *p; return p; }

$ g++-6 wtf.cpp -c 
wtf.cpp: In function ‘void* operator new(std::size_t)’:
wtf.cpp:4:7: error: declaration of ‘void* operator new(std::size_t) throw (std::bad_alloc)’ has a different exception specifier
 void *operator new(std::size_t) throw (std::bad_alloc) { void * p; return p; }
       ^~~~~~~~
wtf.cpp:3:7: note: from previous declaration ‘void* operator new(std::size_t)’
 void *operator new(std::size_t) throw(std::bad_alloc);
Run Code Online (Sandbox Code Playgroud)

c++ g++ exception throw

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

C++ 11的不同编译器行为

以下代码

#include <vector>
#include <complex>
#include <algorithm>

template<class K>
inline void conjVec(int m, K* const in) {
    static_assert(std::is_same<K, double>::value || std::is_same<K, std::complex<double>>::value, "");
    if(!std::is_same<typename std::remove_pointer<K>::type, double>::value)
#ifndef OK
        std::for_each(in, in + m, [](K& z) { z = std::conj(z); });
#else
        std::for_each(reinterpret_cast<std::complex<double>*>(in), reinterpret_cast<std::complex<double>*>(in) + m, [](std::complex<double>& z) { z = std::conj(z); });
#endif
}

int main(int argc, char* argv[]) {
    std::vector<double> nums;
    nums.emplace_back(1.0);
    conjVec(nums.size(), nums.data());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

用Linux编译好

  1. Debian clang版本3.5.0-9
  2. gcc版本4.9.1
  3. icpc版本15.0.1

在Mac OS X上

  1. gcc版本4.9.2

但没有

  1. 铛,600.0.56
  2. icpc版本15.0.1

除非 …

g++ icc complex-numbers c++11 clang++

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

标签 统计

g++ ×2

c++ ×1

c++11 ×1

clang++ ×1

complex-numbers ×1

exception ×1

icc ×1

throw ×1