c++17

clarification of specifics of P0137

巧了我就是萌 提交于 2019-11-30 02:37:00
问题 In the following code I have been meticulous in the following of the standard's words (plus in the light of the wording of P0137) on object lifetimes. Note that all memory allocation is through suitably-aligned storage of type unsigned char, as per P0137. Note also that Foo is a POD, with a trivial constructor. Questions: A. If I have misunderstood the standard, and there is any UB here, please kindly point it out (or alternatively confirm that there is no UB) B. Are the initialisations at A,

Is it possible to check if a user literal is defined for given type and argument?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 01:51:24
问题 I want to check at compile-time if user literal _name is defined for type Ret and argument Arg . While I have half-solution, it requires the literal operator to be defined at least once: #include <iostream> #include <type_traits> struct one { }; struct two { }; // we need at least one of these definitions for template below to compile one operator"" _x(char const*) {return {};} two operator"" _x(unsigned long long int) {return {};} template<class T, class S, class = void> struct has_literal_x

How to count the number of files in a directory using standard?

半腔热情 提交于 2019-11-30 01:32:04
问题 The new standard expected for 2017 adds std::filesystem . Using it, how can I count the number of files (including sub-directories) in a directory? I know we can do: std::size_t number_of_files_in_directory(std::filesystem::path path) { std::size_t number_of_files = 0u; for (auto const & file : std::filesystem::directory_iterator(path)) { ++number_of_files; } return number_of_files; } But that seems overkill. Does a simpler and faster way exist? 回答1: I do not think that a way to easily get

Conversion from std::string_view to std::string is not implicit. What the hell was the committee thinking?

和自甴很熟 提交于 2019-11-30 01:24:42
问题 Seriously, whats't up. There is an implicit conversion from std::string to std::string_view and it's not considered unsafe. Even though this surely may cause a lot of dangling references if programmer is not careful. On the other hand, they have dismissed an implicit conversion from std::string_view to std::string using same argument but in completely opposite fashion: because programmer may be not careful . It's lovely that they have created a replacement for a raw const char* pointer while

Can I use std::pair, but rename .first and .second member names?

吃可爱长大的小学妹 提交于 2019-11-30 01:13:46
问题 A common design problem I run into, is that I bundle two variables together and then lose the ability to reference them in a meaningful way. std::pair<int,int> cords; cord.first = 0; //is .first the x or y coordinate? cord.second = 0; //is .second the x or y coordinate? I've considered writing basic structs instead, but then I lose a lot of the benefits that come along with std::pair : make_pair non-member overloaded operators swap get etc. Is there a way to rename or provide an alternative

C++1z why not remove digraphs along with trigraphs?

≯℡__Kan透↙ 提交于 2019-11-30 01:04:43
问题 C++1z will remove trigraphs. IBM heavily opposed this (here and here) so there seem to be arguments for both sides of removal/non removal. But since the decision was made to remove trigraphs, why leave digraphs? I don't see any reasons for keeping digraphs beyond the reasons to keep trigraphs (which apparently didn't weight enough to keep them). 回答1: Trigraphs are more problematic to the unaware user than digraphs. This is because they are replaced within string literals and comments. Here

Preventing users from creating unnamed instances of a class [duplicate]

£可爱£侵袭症+ 提交于 2019-11-30 00:42:17
问题 This question already has answers here : How to avoid C++ anonymous objects (7 answers) Closed 2 years ago . For many RAII "guard" classes, being instantiated as anonymous variables does not make sense at all: { std::lock_guard<std::mutex>{some_mutex}; // Does not protect the scope! // The unnamed instance is immediately destroyed. } { scope_guard{[]{ cleanup(); }}; // `cleanup()` is executed immediately! // The unnamed instance is immediately destroyed. } From this article: Anonymous

GCC and Clang disagree about C++17 constexpr lambda captures

岁酱吖の 提交于 2019-11-30 00:36:37
问题 Consider this example which declares a variable as constexpr, captures it by copy in a lambda, and declares another constexpr variable which is the result of a constexpr function unwrapping a non-type template parameter from the original variable. #include <utility> template<int I> constexpr auto unwrap(std::integral_constant<int, I>) { return I; } int main() { constexpr auto i = std::integral_constant<int, 42>{}; constexpr auto l = [i]() { constexpr int x = unwrap(i); }; } Clang (trunk)

template argument deduction with strongly-typed enumerations

天涯浪子 提交于 2019-11-30 00:07:22
If I have a normal (weak) enumeration, I can use its enumerated values as non-type template parameters, like so: enum { Cat, Dog, Horse }; template <int Val, typename T> bool magic(T &t) { return magical_traits<Val>::invoke(t); } and call it as: magic<Cat>(t) as far as I can see, if I have a strongly-typed enumeration and don't want to hard-code the enumeration type, I end up with: enum class Animal { Cat, Dog, Horse }; template <typename EnumClass, EnumClass EnumVal, typename T> bool magic(T &t) { return magical_traits<EnumVal>::invoke(t); } and now I have to write: magic<Animal, Animal::Cat>

Is it possible to ensure a constexpr function is called at most once at compile time?

感情迁移 提交于 2019-11-29 22:40:13
问题 As the title asks: Is it possible to ensure a constexpr function is called at most once at compile time? This clearly won't be possible if the function is not constepxr; I could write a function that gets called whenever I press the space bar, so the compiler could never figure that out at compile time. 回答1: Short answer: no, because constexpr functions cannot read/set external state. (They can have internal state, but they still need to be "pure") . Real answer: probably yes, but it's a bad