c++17

Why does using uniform initializer syntax result in different behavior to the “old” style ()?

£可爱£侵袭症+ 提交于 2019-12-08 17:33:53
问题 I get different results if I try to use a uniform initializer for std::set . Example: int main() { std::array a {1,2,3,4}; std::set<int> s1 {a.begin(), a.end()}; std::set s2 {a.begin(), a.end()}; std::set s3 (a.begin(), a.end()); for(auto& i: s1) { std::cout << i << "\n"; } std::cout << "####" << std::endl; for(auto& i: s2) { std::cout << i << "\n"; } std::cout << "####" << std::endl; for(auto& i: s3) { std::cout << i << "\n"; } } Results in: 1 2 3 4 #### 0x7ffecf9d12e0 0x7ffecf9d12f0 #### 1

Are trigraphs still valid C++?

时光毁灭记忆、已成空白 提交于 2019-12-08 17:09:55
问题 We all know about the historical curiosity that is digraphs and trigraphs, but with all the changes made to C++ in recent years I'm curious: are they valid C++14? How about C++17? 回答1: Trigraphs are currently valid, but won't be for long! Trigraphs were proposed for deprecation in C++0x, which was released as C++11. This was opposed by IBM, speaking on behalf of itself and other users of C++, and as a result trigraphs were retained in C++0x. Trigraphs were then proposed again for removal (not

Convert a string to std filesystem path

僤鯓⒐⒋嵵緔 提交于 2019-12-08 16:43:42
问题 A file path is passed as a string. How do I convert this string to a std::filesystem::path? Example: #include <filesystem> std::string inputPath = "a/custom/path.ext"; const std::filesystem::path path = inputPath; // Is this assignment safe? 回答1: Yes, this construction is safe: const std::filesystem::path path = inputPath; // Is this assignment safe? That is not assignment, that is copy initialization. You are invoking this constructor: template< class Source > path( const Source& source );

C++ block scope extern declaration linkage, confusing C++ standard explanation

时间秒杀一切 提交于 2019-12-08 16:41:19
问题 Standards N3242 (C++ 11 draft) and N3797 (C++ 14 draft) both have the same paragraph. § 3.5 Program and linkage [basic.link] ¶ 6 The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives

Any advantage of using the s suffix in C++ [duplicate]

假如想象 提交于 2019-12-08 16:04:41
问题 This question already has answers here : Advantages of using user-defined literal for strings instead of string literal (4 answers) Closed last year . My question is related to the use of the "s" suffix in C++? Example of code using the "s" suffix: auto hello = "Hello!"s; // a std::string The same could be written as: auto hello = std::string{"Hello!"}; I was able to find online that the "s" suffix should be used to minimizes mistakes and to clarify our intentions in the code. Therefore, is

g++ c++17 class template argument deduction not working in a very specific case

你。 提交于 2019-12-08 15:53:20
问题 I have the following code: template <class T> class lit { public: lit(T l) : val(l) {} T val; }; template <class T> class cat { public: cat(lit<T> const& a, lit<T> const& b) : a(a), b(b) {} lit<T> const& a; lit<T> const& b; }; template <class T> cat<T> operator+(lit<T> const& a, lit<T> const& b) { return cat(a, b); } int main() { auto r1 = cat((lit ('b')), lit('d')); // compiles auto r2 = (lit ('b')) + lit('d') ; // doesn't compile auto r3 = lit ('b') + lit('d') ; // compiles auto r4 = (lit (

Force template instantiation via typedef : success at g++ , fail at Visual C++

≡放荡痞女 提交于 2019-12-08 15:32:01
问题 I want to force template instantiation. The below code works (print 1 ) at g++ ( http://coliru.stacked-crooked.com/a/33986d0e0d320ad4 ). However, it prints wrong result ( 0 ) at Visual C++ ( https://rextester.com/WGQG68063 ). #include <iostream> #include <string> template <int& T>struct NonTypeParameter { }; //internal implementation int lala=0; template <typename T> struct Holder{ static int init; }; template <typename T> int Holder<T>::init = lala++; //tool for user template <typename T>

If structured bindings cannot be constexpr why can they be used in constexpr function?

我们两清 提交于 2019-12-08 15:13:20
问题 According to this answer apparently there is no good reason why structured bindings are not allowed to be constexpr, yet the standard still forbids it. In this case, however, shouldn't the use of the structured bindings inside the constexpr function also be prohibited? Consider a simple snippet: #include <utility> constexpr int foo(std::pair<int, int> p) { auto [a, b] = p; return a; } int main() { constexpr int a = foo({1, 2}); static_assert(a == 1); } Both gcc and clang does not cause

Is a lambda in a default template parameter considered part of the immediate context?

情到浓时终转凉″ 提交于 2019-12-08 14:58:05
问题 Is the following code well-formed C++17? template <typename T, int = [](auto t) { decltype(t)::invalid; return 0; }(T{})> constexpr int f(T) { return 0; } constexpr int f(...) { return 1; } static_assert(f(0) == 1); clang and edg accept it, while msvc and gcc 1 reject it. I can't find anything that would say that this is a hard error, but I also can't find anything that would say that this is a deduction failure. In C++20, there is this paragraph ([temp.deduct]p9): A lambda-expression

Reinterpret cast a template non-type parameter: clang c++14 vs c++1z

倖福魔咒の 提交于 2019-12-08 14:55:47
问题 Consider the following code: template <int* > struct foo { }; int main() { foo<(int*)42> f; (void)f; } When compiling on clang 3.8.0 with -std=c++11 or -std=c++14 , the program compiles. When compiling with -std=c++1z , it errors with: main.cpp:4:9: error: non-type template argument is not a constant expression foo<(int*)42> f; ^~~~~~~~ gcc 5.3.0 does not compile the code regardless of C++ mode, which I believe to be correct. What is the difference in clang between C++14 and C++1z and why