clang++

Detect dangling references to temporary

坚强是说给别人听的谎言 提交于 2019-11-30 06:18:27
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 simple example to illustrate it). Because of UB all clang compiler since 3.9 starts to reuse this

clang 4 build error on <functional> with c++1z

喜你入骨 提交于 2019-11-30 03:24:12
问题 I just updated my arch linux system to the latest which includes gcc 7.1.1. Trying to build this: #include <functional> int main(int argc, char** argv) { return 1; } using the command clang++ main.cpp -std=c++1z results in the error: In file included from main.cpp:1: In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/functional:60: In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../include/c++/7.1.1/unordered_map

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

南笙酒味 提交于 2019-11-29 21:21:30
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: static_assert (is_int<Int<0>>::value, ""); ^ $ The problem is caused by different integral template

clang++ -stdlib=libc++ leads to undefined reference

纵然是瞬间 提交于 2019-11-29 18:41:17
问题 Why am I getting the following linker error when using clang with libc++: $ clang++ -stdlib=libc++ po.cxx -lpoppler /tmp/po-QqlXGY.o: In function `main': po.cxx:(.text+0x33): undefined reference to `Dict::lookup(char*, Object*, std::__1::set<int, std::__1::less<int>, std::__1::allocator<int> >*)' clang: error: linker command failed with exit code 1 (use -v to see invocation) Where: $ nm -D /usr/lib/x86_64-linux-gnu/libpoppler.so | grep lookup | c++filt| grep \ Dict::lookup\( 00000000000c1870

Rcpp and default C++ compiler

时光毁灭记忆、已成空白 提交于 2019-11-29 18:38:20
问题 I have some strange troubles with Rcpp - it uses unpredictable C++ compiler. This question is somewhat similar to this question. I'm on OSX, I have 2 complilers - default clang and clang-omp with openmp support. Also I have following ~/.R/Makevars file (where I set up clang-omp as default compiler): CC=clang-omp CXX=clang-omp++ CFLAGS += -O3 -Wall -pipe -pedantic -std=gnu99 CXXFLAGS += -O3 -Wall -pipe -Wno-unused -pedantic -fopenmp The problem is that, the package I'm developing compiles with

How to explicitly call a namespace-qualified destructor?

孤者浪人 提交于 2019-11-29 16:09:15
问题 I am surprised that the following simple code won't compile (with gcc, version 4.8.1) #include <string> void test() { std::string* p = new std::string("Destruct me"); p->std::~string(); } It says: error: scope ‘std’ before ‘~’ is not a class-name. Yet reading the standard, I would say the syntax says it should be " postfix-expresssion -> pseudo-constructor-name ", where pseudo-constructor-name can be of the form " nested-name-specifier ~ type-name ", and nested-name-specifier can be "

Clang Compile error with default initialization [duplicate]

浪子不回头ぞ 提交于 2019-11-29 14:31:09
This question already has an answer here: Do I really need to implement user-provided constructor for const objects? 4 answers Consider following example: #include <iostream> #include <type_traits> struct A { //A() = default; // does neither compile with, nor without this line //A(){}; // does compile with this line int someVal{ 123 }; void foobar( int ) { }; }; int main() { const A a; std::cout << "isPOD = " << std::is_pod<A>::value << std::endl; std::cout << "a.someVal = " <<a.someVal << std::endl; } See Live example This does compile with g++ but does not compile with clang++, tried with

g++ rejects, clang++ accepts: foo(x)(“bar”)(“baz”);

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:07:42
Somebody had asked the other day why something compiles with clang, but not with gcc. I intuitively understood what was happening and was able to help the person, but it got me wondering -- according to the standard, which compiler was correct? Here is a boiled down version of the code: #include <iostream> #include <string> class foo { public: foo(const std::string& x): name(x) { } foo& operator()(const std::string& x) { std::cout << name << ": " << x << std::endl; return (*this); } std::string name; }; int main() { std::string x = "foo"; foo(x)("bar")("baz"); return 0; } This compiles fine

get<string> for variants fail under clang++ but not g++

流过昼夜 提交于 2019-11-29 13:07:41
The following code: variant<string> x = "abc"; cout << get<string>(x) << "\n"; works fine under g++ (version 7.2). However, when compiled under clang++ (version 5.0) using libstdc++, I get the following error in the get method: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../include/c++/7.2.0/variant:238:46: fatal error: cannot cast 'std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to its private base class 'std::__detail::__variant::_Variant_storage<false, std:: __cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>

Parameter with non-deduced type after parameter pack

无人久伴 提交于 2019-11-29 11:23:21
There is different behaviour in clang++ and g++ for the next program: #include <type_traits> #include <utility> template< std::size_t index, typename type > struct ref { type & value; }; template< std::size_t index, typename type > type && get(ref< index, type > const & r) { return std::forward< type >(r.value); } template< typename F, typename ...types, std::size_t ...indices > decltype(auto) apply_inverse(F & f, types &... values, std::index_sequence< indices... >) { struct : ref< indices, types >... {} refs{{values}...}; constexpr std::size_t top = sizeof...(indices) - 1; return std: