c++17

Short circuit of overloaded operator && and || in C++17

冷暖自知 提交于 2019-12-21 07:45:34
问题 I read in http://en.cppreference.com/w/cpp/language/operators: The boolean logic operators, operator && and operator || Unlike the built-in versions, the overloads do not sequence their left operand before the right one, and (until C++17) cannot implement short-circuit evaluation. (My emphasis). Couldn't find any resource or code example for C++17 supporting short-circuit for operator&& and operator||. Is it related to C++17 parameter pack fold expression? tried to play with it but couldn't

Short circuit of overloaded operator && and || in C++17

谁说我不能喝 提交于 2019-12-21 07:44:54
问题 I read in http://en.cppreference.com/w/cpp/language/operators: The boolean logic operators, operator && and operator || Unlike the built-in versions, the overloads do not sequence their left operand before the right one, and (until C++17) cannot implement short-circuit evaluation. (My emphasis). Couldn't find any resource or code example for C++17 supporting short-circuit for operator&& and operator||. Is it related to C++17 parameter pack fold expression? tried to play with it but couldn't

Constructor called on return statement

落花浮王杯 提交于 2019-12-21 07:41:23
问题 Consider the following example: class X { public: X() = default; X(const X&) = default; X(X&&) = delete; }; X foo() { X result; return result; } int main() { foo(); } Clang and GCC disagree on whether this program is valid. GCC tries to call the move constructor when initializing the temporary during the call to foo() , which has been deleted leading to a compilation error. Clang handles this just fine, even with -fno-elide-constructors . Can anyone explain why GCC is allowed to call the move

Ambiguous partial specializations with Clang in C++17

落爺英雄遲暮 提交于 2019-12-21 07:36:25
问题 template <typename Foo, Foo Part> struct TSelect {}; enum What { The }; template <typename Foo> struct AnotherOneSelector { static constexpr Foo Id = Foo::The; }; template <typename Foo, typename SelectPartType> struct THelper; template <typename Foo> struct THelper<Foo, TSelect<Foo, AnotherOneSelector<Foo>::Id>> {}; template <typename Foo, Foo PartId> struct THelper<Foo, TSelect<Foo, PartId>> {}; int main() { THelper<What, TSelect<What, What::The>> t; } This code compiles with gcc8.1 with

Is there a way to reset a std::variant from a known alternative?

落爺英雄遲暮 提交于 2019-12-21 07:34:51
问题 I'm in the process of updating a codebase that is currently using a custom equivalent of std::variant to C++17 . In certain parts of the code, the variant is being reset from a known alternative, so the class provides a method that asserts that index() is at a current value, but still directly invokes the proper destructor unconditionally. This is used in some tight inner loops, and has (measured) non-trivial performance impact. That's because it allows the compiler to eliminate the entire

g++ and clang++ different behaviour deducing variadic template `auto` values

半城伤御伤魂 提交于 2019-12-21 07:22:36
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

g++ and clang++ different behaviour deducing variadic template `auto` values

南楼画角 提交于 2019-12-21 07:21:47
问题 Another "who's right between g++ and clang++?" This time I'm convinced it's a g++ bug, but I ask for a confirm from standard gurus. Given the following code template <template <auto...> class Cnt, typename ... Types, Types ... Vals> void foo (Cnt<Vals...>) { } template <auto ...> struct bar { }; int main () { foo(bar<0, 1>{}); // compile both foo(bar<0, 1L>{}); // only clang++ compile; error from g++ } Live demo clang++ (8.0.0, by example) compile and link without problem where g++ (9.2.0, by

C++17 lambda capture *this

穿精又带淫゛_ 提交于 2019-12-21 06:50:16
问题 C++17 will add copy capture of this object by value, with a capture specification of [*this]. How is this useful? How is it different than capturing this ? Can't this already be achieved in C++14 with [tmp = *this] ? Bonus for explaining why P0018R3 uses [=, tmp = *this] instead of [tmp = *this] in their example. If they had used [tmp = *this] , all the listed downsides of the C++14 solution would be eliminated. 回答1: How is it useful? It's useful when you need a copy of *this - for example,

VS2017: E0135 namespace “std” has no member “filesystem”

℡╲_俬逩灬. 提交于 2019-12-21 06:49:49
问题 In order to use: std::filesystem from the C++17 library, my project was migrated from vs2015 to vs2017. My project compiles and runs without error, the lib is included without error, but when trying to use std::filesystem I get the following: It seems the library is not being included but cant see why not? Edit: Microsoft Visual Studio Enterprise 2017 VisualStudio.15.Release/15.7.3+27703.2026 Visual C++ 2017 00369-90000-00000-AA466 Microsoft Visual C++ 2017 回答1: A couple of options to

std::function with noexcept in C++17

北战南征 提交于 2019-12-21 06:48:43
问题 In C++17 noexcept has been added to the type system: void r1( void (*f)() noexcept ) { f(); } void foo() { throw 1; } int main() { r1(foo); } The latest versions of GCC and Clang in C++17 mode reject the call r1(foo) , because void (*)() cannot be implicitly converted to void (*)() noexcept . But with std::function instead: #include <functional> void r2( std::function<void() noexcept> f ) { f(); } void foo() { throw 1; } int main() { r2(foo); } Clang accepts the program, apparently ignoring