c++20

Will consteval functions allow template parameters dependent on function arguments?

五迷三道 提交于 2020-03-10 10:48:31
问题 In C++17, this code is illegal: constexpr int foo(int i) { return std::integral_constant<int, i>::value; } That's because even if foo can be evaluated at compile-time, the compiler still needs to produce the instructions to execute it at runtime, thus making the template instantiation impossible. In C++20 we will have consteval functions, which are required to be evaluated at compile-time, so the runtime constraint should be removed. Does it mean this code will be legal? consteval int foo(int

how std::u8string will be different from std::string?

落爺英雄遲暮 提交于 2020-02-27 22:19:50
问题 If I have a string: std::string s = u8"你好"; and in C++20, std::u8string s = u8"你好"; how std::u8string will be different from std::string ? 回答1: Since the difference between u8string and string is that one is templated on char8_t and the other on char , the real question is what is the difference between using char8_t -based strings vs. char -based strings. It really comes down to this: type-based encoding. Any char -based string ( char* , char[] , string , etc) may be encoded in UTF-8. But

What is the purpose of C++20 std::common_reference?

我与影子孤独终老i 提交于 2020-02-17 07:00:31
问题 C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it? 回答1: common_reference came out of my efforts to come up with a conceptualization of STL's iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type . The former is the return type of the iterator's operator* , and the value_type is the (non-const, non-reference) type of the elements of the sequence. Generic

What is the purpose of C++20 std::common_reference?

走远了吗. 提交于 2020-02-17 06:57:10
问题 C++20 introduces std::common_reference. What is its purpose? Can someone give an example of using it? 回答1: common_reference came out of my efforts to come up with a conceptualization of STL's iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type . The former is the return type of the iterator's operator* , and the value_type is the (non-const, non-reference) type of the elements of the sequence. Generic

How to parse json file with type composition of std::optional and std::variant

谁都会走 提交于 2020-02-16 12:28:30
问题 How can I parse the input json inside this file, especially for the accuracy , secondary and flags properties? https://github.com/smogon/pokemon-showdown/blob/master/data/moves.js They are type composition of optional and variant . Though only the full example can surface the problem in a clearer way, a minimal starting point example would be this one: [ {}, { "secondary": false }, { "secondary": { "chance": 10, "boosts": { "spd": -1 } } }, { "secondary": { "chance": 30, "volatileStatus":

outputting char8_t const* to cout and wcout, one compiles one not

佐手、 提交于 2020-02-05 06:55:26
问题 Since P1423R1 adds deleted ostream inserters for char8_t, char16_t, and char32_t, we are momentarily left in the situation that we need to write custom operators if we wish to stream these types to ostreams. When attempting to do this for MSVC 2019 16.2.0 Preview 2.0. #include <iostream> #include <string> using namespace std::literals; template<typename Tostream> Tostream& operator<<( Tostream& os, std::u8string_view string ) { return os; } template<typename Tostream> Tostream& operator<<(

How to convert in modern C++ a double into a datetime

巧了我就是萌 提交于 2020-01-24 22:10:11
问题 How to convert in modern C++ (C++11/14/17) a double into a date time using the date.h library, when the double has been generated while exporting an Excel worksheet as a CSV file? For instance, the datetime appearing in Excel: 21/08/2017 11:54 has been converted by Excel into the CSV file as the double: 42968.4958333333 Thanks. EDIT on 11/07/2019: This questions is about the use of the date.h library. The other questions pointed out as "possible duplicates" does not require the use of this

Using range-v3 to read comma separated list of numbers

半腔热情 提交于 2020-01-24 09:28:46
问题 I'd like to use Ranges (I use range-v3 implementation) to read a input stream that is a comma separated list of numbers. That is trivial to do without ranges but... This is what I thought was the straight-forward way to solve it: auto input = std::istringstream("42,314,11,0,14,-5,37"); auto ints = ranges::istream_view<int>(input) | ranges::view::split(","); for (int i : ints) { std::cout << i << std::endl; } But this fails to compile. I've tried a number of variations of this but nothing seem

Will getting the current date/time be thread-safe in C++20?

混江龙づ霸主 提交于 2020-01-23 04:43:33
问题 Short Question Up to and including C++17, C++ provides no thread-safe way to get the current time or date. Will this be fixed in C++20? Long Question The only portable way to get the current time and date is by using the std::gmtime or std::localtime functions. Remnants from the early days of C, these functions convert a given time since an implementation-defined epoch into calender time (for example, 1515153600 into Fri, 05 Jan 2018 12:00:00 GMT). The only downside, however, is that those

Will consteval allow using static_assert on function arguments?

社会主义新天地 提交于 2020-01-22 18:26:23
问题 Currently you cannot use static_assert to verify parameters of a constexpr function, even if all calls to it are indeed constexpr . That makes sense because the compiler still has to create a non-constexpr instantiation of this function in case some other module will try to call it. Sadly, this is the case even if the function is static or in an anonymous namespace. C++20 however, will introduce a new keyword consteval which is like constexpr but it doesn't allow calling a function in a non