c++17

Constexpr if with a non-bool condition

荒凉一梦 提交于 2019-12-04 23:37:13
I seem to have found something that Clang and GCC disagree on. Here's the code: int main() { if constexpr (2) {} } This successfully compiles with GCC 7.4.0, but it fails with Clang 7.0.0 with this error message: test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool' [-Wc++11-narrowing] if constexpr (2) {} ^ 1 error generated. cppreference doesn't seem to mention "narrowing", so this seems like a Clang bug, but I'm not entirely certain. If this is a bug with either compiler, has it already been reported? StoryTeller - Unslander Monica Clang is

Is the move constructor called after invoking a conversion function?

眉间皱痕 提交于 2019-12-04 22:48:24
Consider this example: struct T { }; struct S { operator T(); }; S s; T t = s; [dcl.init] will take us to [over.match.copy] which will find the conversion function operator T() . But are we done at that point, or do we have to invoke T(T&& rhs) , binding rhs to the return of operator T() via [dcl.init.ref]? Are there any differences with regards to the answer to this question between C++11 and C++1z? This falls under [dcl.init]/17.6.3 , which is pretty clear about what happens after overload resolution selects the conversion function: The function selected is called with the initializer

Why does std::visit take a variable number of variants?

泄露秘密 提交于 2019-12-04 22:30:37
Trying to get more familiar with C++17, I've just noticed std::visit : template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple visitors and using them on the same variant. So, why this specific 'variadification'? To make multiple

Variable templates and std::cout — order of construction

南笙酒味 提交于 2019-12-04 22:24:08
It looks like we can safely use std::cout object in constructors of objects with static storage duration as stated in this question . However, I'm not entirely sure that we can safely use them in case of variable templates: #include <iostream> template<class T> T x = T{}; void foo() { class Test { public: Test() { std::cout << "Test::Test\n"; } }; Test t = x<Test>; } int main() { std::cout << "main\n"; } This code crashes in clang ( live example ) and I'm not sure whether it's a bug or not. As explained in that question, one effect of #include <iostream> is the equivalent of defining a global

CMake: Replace compile flags of an INTERFACE target

北城以北 提交于 2019-12-04 22:17:43
I need to replace the /std:c++14 flag of an INTERFACE target (header-only library) with /std:c++17 . CMake doesn't support setting C++17 flags in VS directly yet (see How to enable /std:c++17 in VS2017 with CMake ) so I need to manually replace it. However get_target_property(my_compile_flags mylib COMPILE_OPTIONS) to retrieve the list of currently set flags to then subsequently replace /std:c++14 with /std:c++17 doesn't work: INTERFACE_LIBRARY targets may only have whitelisted properties. The property "COMPILE_OPTIONS" is not allowed. You can set them however via target_compile_features(...)

How to swap two pointers in multi-threaded c++ 17 program?

江枫思渺然 提交于 2019-12-04 21:32:13
I have two pointers: pA and pB. They points to two big hash map objects. when the hash map pointed by pB is updated completely, I want to swap pB and pA. In C++ 17, how to swap them fast and thread safe? Atomic? I am new to c++ 17. Atomic wait-free exchange of 2 pointers can be implemented in the following manner: #include <atomic> #include <cstdint> #include <cassert> template<class T> class Pointers2 { uintptr_t const ab_; std::atomic<uintptr_t> a_; public: Pointers2(T* a, T* b) : ab_(reinterpret_cast<uintptr_t>(a) ^ reinterpret_cast<uintptr_t>(b)) , a_(reinterpret_cast<uintptr_t>(a)) {} T*

`constexpr` variable “used in its own initializer”: Clang vs. GCC

纵然是瞬间 提交于 2019-12-04 20:36:58
问题 This question seems related to an existing one, but I do not understand the "portable workaround" provided in the answer there (involving const auto this_ = this; ) and moreover I think the following example is easier to follow. I am playing with the following snippet of C++17 code (live demo): #include <iostream> struct Test { const char* name_{nullptr}; const Test* src_{nullptr}; constexpr Test(const char* name) noexcept : name_{name} {} constexpr Test(const Test& src) noexcept : src_{&src}

C++17: still using enums as constants? [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-04 18:59:07
问题 This question already has answers here : Replacing constants: when to use static constexpr and inline constexpr? (2 answers) Closed 10 months ago . I am used to using enum as constants -- they're quick to write, can be placed in .h files, and work fine. enum {BOX_LEFT=10, BOX_TOP=50, BOX_WIDTH=100, BOX_HEIGHT=50}; enum {REASONS_I_LIKE_ENUM_AS_CONSTANTS = 3}; Is this no longer a good idea? I see good reasons to prefer enum class (conventional enums implicitly convert to int; conventional enums

Compiletime for each with custom functions

*爱你&永不变心* 提交于 2019-12-04 18:39:00
Abstract: Imagine a problem of the following form: One has to invoke multiple specific member functions with the same parameters on a list of functors. That makes a good problem to solve with an interface (runtime_interface, in other words a requirement of functions that those functors have to implement). The Problem I would like to discuss is the case where the list of functors is known at compile time, but might be subject to change during the further development process. Because in this case if implemented like that one is paying the runtime overhead even though all the functions to be

Infinity not constexpr

[亡魂溺海] 提交于 2019-12-04 17:51:21
问题 I wanted to test the behavior of floats near infinity. For that I naively wrote the following code: #include <limits> #include <iostream> int main() { constexpr float foo = std::numeric_limits<float>::infinity() - std::numeric_limits<float>::epsilon(); std::cout << foo << std::endl; return foo; } The interesting part to me was that this compiles fine in GCC 7.2 but fails on Clang 5 (complaining about non-constexpr assign of foo ). AFAIK, since C++11, std::numeric_limits<float>::infinity() and