Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using clang's feature checking macros to detect existence of std::launder

Tags:

c++

c++17

clang++

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.

like image 366
JannisW Avatar asked Apr 07 '18 09:04

JannisW


2 Answers

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
like image 84
Daniel Jour Avatar answered Oct 09 '22 02:10

Daniel Jour


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...

like image 20
Marc Glisse Avatar answered Oct 09 '22 02:10

Marc Glisse