c++20

Portably detect __VA_OPT__ support?

只愿长相守 提交于 2020-01-12 02:50:29
问题 In C++20, the preprocessor supports __VA_OPT__ as a way to optionally expand tokens in a variadic macro if the number of arguments is greater than zero. (This obviates the need for the ##__VA_ARGS__ GCC extension, which is a non-portable and ugly hack.) Clang SVN has implemented this feature, but they haven't added a feature test macro for it. Can any clever preprocessor hacker figure out a way to detect the presence or absence of __VA_OPT__ support without causing a hard error or a

Can consteval functions from different translation units interfere?

安稳与你 提交于 2020-01-06 05:46:07
问题 I am trying to dig into implications of a function being inline and stumbled upon this issue. Consider this small program (demo): /* ---------- main.cpp ---------- */ void other(); constexpr int get() { return 3; } int main() { std::cout << get() << std::endl; other(); } /* ---------- other.cpp ---------- */ constexpr int get() { return 4; } void other() { std::cout << get() << std::endl; } When compiled without optimizations, the program yields the following output: 3 3 Which might be not

Why is constexpr with std::forward_as_tuple not working? [duplicate]

允我心安 提交于 2020-01-05 07:11:27
问题 This question already has answers here : how to initialize a constexpr reference (3 answers) Constexpr Class taking const references not compiling (1 answer) constexpr begin of a std::array (1 answer) Closed 2 days ago . Why is the following not compiling? This is somehow counter-intuitive (not to say constexpr concepts are confusing): #include <tuple> int main() { constexpr const int a = 0; static_assert(a == 0, "Wups"); constexpr auto t2 = std::forward_as_tuple(a, a); } LIVE I assumed that

Why does the upcoming Ranges library not support container initialization from a range?

女生的网名这么多〃 提交于 2020-01-04 04:56:43
问题 Introduction With the upcoming Ranges library, the need to denote a range with two iterators is pretty much gone. For example, instead of if (std::equal(begin(foo), end(foo), begin(bar), end(bar))) we have if (std::ranges::equal(foo, bar)) The latter is arguably superior not only because of its conciseness, but also because it prevents the common pitfall of omitting end(bar) and welcoming bound errors. Problem How about the following code? std::vector<int> vec{begin(foo), end(foo)}; where foo

Ramifications of C++20 requiring two's complement

╄→尐↘猪︶ㄣ 提交于 2020-01-01 08:35:15
问题 C++20 will specify that signed integral types must use two's complement. This doesn't seem like a big change given that (virtually?) every implementation currently uses two's complement. But I was wondering if this change might shift some "undefined behaviors" to be "implementation defined" or even "defined." Consider, the absolute value function, std::abs(int) and some of its overloads. The C++ standard includes this function by reference to the C standard, which says that the behavior is

Calling non-static member function outside of object's lifetime in C++17

谁说胖子不能爱 提交于 2019-12-31 11:24:04
问题 Does the following program have undefined behavior in C++17 and later? struct A { void f(int) { /* Assume there is no access to *this here */ } }; int main() { auto a = new A; a->f((a->~A(), 0)); } C++17 guarantees that a->f is evaluated to the member function of the A object before the call's argument is evaluated. Therefore the indirection from -> is well-defined. But before the function call is entered, the argument is evaluated and ends the lifetime of the A object (see however the edits

Calling non-static member function outside of object's lifetime in C++17

早过忘川 提交于 2019-12-31 11:22:27
问题 Does the following program have undefined behavior in C++17 and later? struct A { void f(int) { /* Assume there is no access to *this here */ } }; int main() { auto a = new A; a->f((a->~A(), 0)); } C++17 guarantees that a->f is evaluated to the member function of the A object before the call's argument is evaluated. Therefore the indirection from -> is well-defined. But before the function call is entered, the argument is evaluated and ends the lifetime of the A object (see however the edits

Deducing a user-defined-value template argument (C++2a, P0732R2)

百般思念 提交于 2019-12-30 05:08:28
问题 I am trying to get the value of a template parameter of a user-defined class deduced (http://wg21.link/p0732r2), using GCC 9.1 with -std=c++2a. struct user_type { int a; constexpr user_type( int a ): a( a ){} }; template< user_type u > struct value {}; template< user_type u > void f( value< u > arg ){} void g(){ f( value< user_type( 0 ) >() ); // error here } compiler explorer: https://godbolt.org/z/6v_p_R I get the error: source>:8:30: note: template argument deduction/substitution failed:

template with lambda as unique default parameter on each instantiation

旧城冷巷雨未停 提交于 2019-12-30 00:35:13
问题 I'm looking for a way to automatically make default template parameter be unique each time a template is instantiated. Since unnamed function objects created by lambda expressions have different types I thought of adopting them somehow. With recent changes to standard daft removing "A lambda-expression shall not appear in ... a template-argument" restriction (see Wording for lambdas in unevaluated contexts) it seemed like a good idea. So I wrote the following kinda working snippet that

How can I Populate a chrono::year With the Current Year?

↘锁芯ラ 提交于 2019-12-22 08:49:22
问题 So I understand from this question that the integer used in the construction of a chrono::year corresponds to the Anno Domini origin of 0. So my question is, what if I wanted to get the current chrono::year . Is there a function for that? I can obviously do: const auto time = std::time(nullptr); const auto current_date = *std::gmtime(&time); const chrono::year foo{ current_date.tm_year + 1900 }; But that seems like a pretty convoluted process. Is there anything better available to me? 回答1: