clang++

Standard-layout and tail padding

会有一股神秘感。 提交于 2019-11-29 11:12:39
问题 David Hollman recently tweeted the following example (which I've slightly reduced): struct FooBeforeBase { double d; bool b[4]; }; struct FooBefore : FooBeforeBase { float value; }; static_assert(sizeof(FooBefore) > 16); //---------------------------------------------------- struct FooAfterBase { protected: double d; public: bool b[4]; }; struct FooAfter : FooAfterBase { float value; }; static_assert(sizeof(FooAfter) == 16); You can examine the layout in clang on godbolt and see that the

Constexpr compile error using std::acos with clang++ not g++

ⅰ亾dé卋堺 提交于 2019-11-29 10:29:50
I want to experiment with migrating a project from gcc to clang++. I admit ignorance on my part, I'm not sure why the following bit of code template <typename T> constexpr T pi{std::acos(T(-1.0))}; compiles silently with g++ but clang++ produces the error trig.hpp:3:13: error: constexpr variable 'pi<float>' must be initialized by a constant expression constexpr T pi{std::acos(T(-1.0))}; and I was hoping someone who knows more about it than I do could enlighten me. NB: Tried with -std=C++14 and C++1y. Fails under clang version 3.6.2 (tags/RELEASE_362/final). Works with g++ (GCC) 5.2.0. Shafik

Explicit instantiation of templated constructor for template class

假如想象 提交于 2019-11-29 09:34:14
I am uncertain if it is a bug in Clang 3.2 or a violation of C++03, but it appears that explicit instantiation of templated constructors for template classes fails, but explicit instantiation of templated member functions of template classes succeeds. For instance, the following compiles without a problem with both clang++ and g++: template<typename T> class Foo { public: template<typename S> void Bar( const Foo<S>& foo ) { } }; template class Foo<int>; template class Foo<float>; template void Foo<int>::Bar( const Foo<int>& foo ); template void Foo<int>::Bar( const Foo<float>& foo ); template

'auto' not allowed in function prototype with Clang

五迷三道 提交于 2019-11-29 09:14:06
Using Clang 3.5, 3.6, or 3.7, with the flag std=c++1y the following code does not compile : #include <iostream> auto foo(auto bar) { return bar; } int main() { std::cout << foo(5.0f) << std::endl; } The error given is : error: 'auto' not allowed in function prototype I do not have errors using g++ 4.9. Is this error produced because Clang has not yet implemented this functionnality yet or is it because I am not allowed to do that and GCC somehow permits it ? As we see from the ISO C++ discussion mailing: decltype(auto) parameters vs. perfect forwarding auto parameters of non-lambdas is part of

Detect dangling references to temporary

空扰寡人 提交于 2019-11-29 06:03:41
问题 Clang 3.9 extremely reuses memory used by temporaries. This code is UB (simplified code): template <class T> class my_optional { public: bool has{ false }; T value; const T& get_or_default(const T& def) { return has ? value : def; } }; void use(const std::string& s) { // ... } int main() { my_optional<std::string> m; // ... const std::string& s = m.get_or_default("default value"); use(s); // s is dangling if default returned } We have tons of code something like above ( my_optional is just a

What is the supposed behavior of copy-list-initialization in the case of an initializer with a conversion operator?

痞子三分冷 提交于 2019-11-29 02:16:01
class AAA { public: AAA() {} AAA(const AAA&) {} }; class BBB { public: BBB() {} operator AAA() { AAA a; return a; } }; int main() { BBB b; AAA a = {b}; } The above code compiles on g++ and vc++, but not clang++. The traditional syntax AAA a = b; compiles ok on all three. class AAA {}; class BBB { public: BBB() {} operator AAA() { AAA a; return a; } }; int main() { BBB b; AAA a = {b}; } The above code doesn't compile on any of g++, vc++, clang++. The only difference against the first code snippet is that I removed the two user-provided constructors of AAA. Again, the traditional syntax AAA a =

Member not zeroed, a clang++ bug?

我只是一个虾纸丫 提交于 2019-11-28 23:27:45
Consider the following code: class A { public: int i; A() {} }; class B { public: A a; int i; }; int main() { B* p = new B {}; std::cout << p->i << " " << p->a.i << "\n"; } Compiled with -std=c++11 in clang++, p->i turns out to be zero, but p->a.i doesn't. Shouldn't the whole object be zeroed as long as its class doesn't have user-provided constructor? EDIT: Since there are some extensive discussion in the comments, I think it's better to add some excerpt from the standard here: To value-initialize an object of type T means: if T is a (possibly cv-qualified) class type (Clause 9) with a user

Is it safe to create a const reference to result of ternary operator in C++?

吃可爱长大的小学妹 提交于 2019-11-28 23:05:34
There's something quite non-obvious going on in this code: float a = 1.; const float & x = true ? a : 2.; // Note: `2.` is a double a = 4.; std::cout << a << ", " << x; both clang and gcc output: 4, 1 One would naively expect the same value printed twice but this isn't the case. The issue here has nothing to do with the reference. There are some interesting rules dictating the type of ? : . If the two arguments are of different type and can be casted, they will by using a temporary. The reference will point to the temporary of ? : . The example above compiles fine and it might or might not

Function not called in code gets called at runtime

帅比萌擦擦* 提交于 2019-11-28 22:28:22
问题 How can the following program be calling format_disk if it's never called in code? #include <cstdio> static void format_disk() { std::puts("formatting hard disk drive!"); } static void (*foo)() = nullptr; void never_called() { foo = format_disk; } int main() { foo(); } This differs from compiler to compiler. Compiling with Clang with optimizations on, the function never_called executes at runtime. $ clang++ -std=c++17 -O3 a.cpp && ./a.out formatting hard disk drive! Compiling with GCC,

g++ and clang++ different behaviour with integral template parameter

血红的双手。 提交于 2019-11-28 18:50:29
问题 I have the following C++11 code. #include <type_traits> using IntType = unsigned long long; template <IntType N> struct Int {}; template <class T> struct is_int : std::false_type {}; template <long long N> struct is_int<Int<N>> : std::true_type {}; int main() { static_assert (is_int<Int<0>>::value, ""); return 0; } Clang++ 3.3 compiles the code but on g++ 4.8.2 static assertion fails $ g++ -std=c++11 main.cpp main.cpp: In function ‘int main()’: main.cpp:15:5: error: static assertion failed: