I have to compile the same code using different versions of clang. Since the code contains some c++17 features that are not supported by every version of clang, I want to check during compile time if they are supported. As far as I can see, clang's feature checking macros are the right way to go.
My problem specifically arises with std::launder.
I created this minimal example:
#include "iostream"
#if __has_builtin(__builtin_launder)
void test() {
int i = 42;
std::cout << "Should compile: " << std::launder(&i) << std::endl;
}
#else
void test() {
int i = 42;
std::cout << "Should not even compile: " << std::launder(&i) << std::endl;
}
#endif
int main(){
test();
}
If I'm compiling it (clang version 6.0.0, libc++) using clang++ -std=c++1z -stdlib=libc++ -Wall -pedantic test3.cpp && ./a.out
The output is:
Should not even compile: 0x7fff75116f64
Although std::launder is obviously supported, the builtin check does not work. Since it is the same check as in reviews llvm: Implement std::launder, I was assuming the check to be correct.
What am I missing? I feel like it is something super simple, but I'm not seeing it.
Worst case I'd use cmake's try-compile mechanism for this purpose instead, but it seems to be an overkill and I'm still interested in finding out what the actual problem is.
From the link to the review you gave, in the implementation of (the library function) std::launder
:
#ifdef _LIBCPP_COMPILER_HAS_BUILTIN_LAUNDER
return __builtin_launder(__p);
#else
return __p;
#endif
}
std::launder
is there also if there's no builtin. Thus the existence or non-existence of the builtin won't tell you whether std::launder
is there or not.
To test whether you have std::launder
or not (which seems to be what you want) you can either use your configuration system (cmake, autoconf, ...) or try your luck with the new (C++17) feature tests:
#include <new> // important!
#if __cpp_lib_launder >= 201606
// have std::launder for sure
#else
// not sure, could've std::launder either way
#endif
See https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations . You are supposed to test __cpp_lib_launder
, but libc++ doesn't seem to implement that in the version I have...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With