c++17

Why does the false branch of “if constexpr” get compiled?

强颜欢笑 提交于 2020-08-26 10:23:26
问题 Why is this code giving error while compiling? My knowledge (and also this) of " if constexpr " says the else block shouldn't get compiled. if constexpr (true) { int a = 10; } else { int b = 10 } The error is: error: expected ‘,’ or ‘;’ before ‘}’ token Compiler used: g++ version 7.5.0 While compiling I used -std=c++17 flag. P.S. The missing ';' is intentional, just to check whether else is being compiled or not. 回答1: There are 2 separate, but related issues here. Firstly, if constexpr will

while statement with initializer

允我心安 提交于 2020-08-24 10:26:04
问题 C++17 has selection statements with initializer status_code foo() { if (status_code c = bar(); c != SUCCESS) { return c; } // ... } I'd like to write a while -loop and a variable with a scope limited to the loop and initialized only once before the first iteration. // fake example, doesn't compile, is doable in many ways while (bool keep_trying = foo(); keep_trying) { // do stuff if (something) keep_trying = false; } Is there anything for this in C++17 or maybe coming in C++2a? 回答1: P0305R1,

How could std::experimental::source_location be implemented?

删除回忆录丶 提交于 2020-08-24 06:03:06
问题 C++ Extensions for Library Fundamentals, Version 2 (N4564) introduces the type std::experimental::source_location. § 14.1.2 [reflection.src_loc.creation] says: static constexpr source_location current() noexcept; Returns: When invoked by a function call (C++14 § 5.2.2) whose postfix-expression is a (possibly parenthesized) id-expression naming current , returns a source_location with an implementation-defined value. The value should be affected by #line (C++14 § 16.4) in the same manner as

No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view)

本秂侑毒 提交于 2020-08-24 05:12:02
问题 My question is regarding C++17 : http://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view What's the caveat of implicit conversion from std::basic_string to std::basic_string_view that it wasn't included in the interface of the latter? I believe it would greatly improve this class. Especially the family of comparison operators that, also do not accept std::string as neither lhs nor rhs . There is such conversion in library fundamentals TS specification: http://en

No implicit conversion from std::string to std::string_view in C++17 (was in std::experimental::basic_string_view)

余生颓废 提交于 2020-08-24 05:11:07
问题 My question is regarding C++17 : http://en.cppreference.com/w/cpp/string/basic_string_view/basic_string_view What's the caveat of implicit conversion from std::basic_string to std::basic_string_view that it wasn't included in the interface of the latter? I believe it would greatly improve this class. Especially the family of comparison operators that, also do not accept std::string as neither lhs nor rhs . There is such conversion in library fundamentals TS specification: http://en

clang 5: std::optional instantiation screws std::is_constructible trait of the parameter type

本小妞迷上赌 提交于 2020-08-22 08:18:47
问题 A really strange and unexpected behaviour of clang 5 was detected when switching to c++17 and replacing custom std::optional solution with the standard one. For some reason, emplace() was being disabled due to faulty evaluation of a std::is_constructible trait of the parameter class. Some specific preconditions must be satisfied before it reproduces: #include <optional> /// Precondition #1: T must be a nested struct struct Foo { struct Victim { /// Precondition #2: T must have an aggregate

Is std::initializer_list{x, y, z} (CTAD) valid?

久未见 提交于 2020-08-22 03:23:07
问题 When constructing an std::initializer_list<U> explicitly, can the template argument ( U ) be deduced (using class template argument deduction (CTAD), for example)? In other words, I know that the following statements are valid: std::initializer_list<int> x1{1, 2, 3}; std::initializer_list<int> x2 = {1, 2, 3}; auto x3 = std::initializer_list<int>{1, 2, 3}; but are the following statements also valid? std::initializer_list x1{1, 2, 3}; std::initializer_list x2 = {1, 2, 3}; auto x3 = std:

struct to/from std::tuple conversion

落花浮王杯 提交于 2020-08-21 09:46:46
问题 Assuming I have struct and std::tuple with same type layout: struct MyStruct { int i; bool b; double d; } using MyTuple = std::tuple<int,bool,double>; Is there any standartized way to cast one to another? P.S. I know that trivial memory copying can do the trick, but it is alignment and implementation dependent 回答1: Unfortunately there is no automatic way to do that, BUT an alternative is adapt the struct to Boost.Fusion sequence. You do this once and for all for each new class. #include

struct to/from std::tuple conversion

时光怂恿深爱的人放手 提交于 2020-08-21 09:46:10
问题 Assuming I have struct and std::tuple with same type layout: struct MyStruct { int i; bool b; double d; } using MyTuple = std::tuple<int,bool,double>; Is there any standartized way to cast one to another? P.S. I know that trivial memory copying can do the trick, but it is alignment and implementation dependent 回答1: Unfortunately there is no automatic way to do that, BUT an alternative is adapt the struct to Boost.Fusion sequence. You do this once and for all for each new class. #include

Default lambda as templated parameter of a function

若如初见. 提交于 2020-08-20 03:09:20
问题 Consider the following code template<bool b, typename T> void foo(const T& t = []() {}) { // implementation here } void bar() { foo<true>([&](){ /* implementation here */ }); // this compiles foo<true>(); // this doesn't compile } In the case that doesn't compile I get the following errors: error C2672: 'foo': no matching overloaded function found error C2783: 'void foo(const T&)': could not deduce template argument for 'T' I think it's clear what I want to achieve: let foo be called with and