c++17

Too many if/else statements in converting user inputs to types in C++

人走茶凉 提交于 2020-07-18 06:25:19
问题 I have a template class with 3 template arguments. template <class T, class U, class Y> class MyClass {}; I wanna get input from users by CLI arguments, something like ./cli float driver-x load The first arg can be float or double The second arg is a driver name: driver-x , driver-y , ... The third argument is about the action type: load , unload , ... If I want to create a new instance of MyClass based on user inputs, I have to define many if/else statements. Because a user inputs are string

Concise bidirectional static 1:1 mapping of values and types

不羁岁月 提交于 2020-07-17 11:18:13
问题 I'm going to start with how I imagine using the code I'd like to create. It doesn't have to be exactly like this but it's a good example of what I mean by "concise" in the title. In my case it's mapping of a type to a related enumeration value. struct bar : foo<bar, foo_type::bar> { /* ... */ }; // \_/ \___________/ // ^ Type ^ Value What this should ideally do is an automatic registration of a bidirectional mapping between the first template parameter of foo , a type, and second, a value,

Concise bidirectional static 1:1 mapping of values and types

╄→尐↘猪︶ㄣ 提交于 2020-07-17 11:16:41
问题 I'm going to start with how I imagine using the code I'd like to create. It doesn't have to be exactly like this but it's a good example of what I mean by "concise" in the title. In my case it's mapping of a type to a related enumeration value. struct bar : foo<bar, foo_type::bar> { /* ... */ }; // \_/ \___________/ // ^ Type ^ Value What this should ideally do is an automatic registration of a bidirectional mapping between the first template parameter of foo , a type, and second, a value,

Why is “inline” required on static inline variables?

南楼画角 提交于 2020-07-17 09:39:27
问题 C++17 allows static member variables to be defined thus: class X { public: static inline int i = 8; }; What is the rationale behind requiring the inline specification? Why not simply allow programmers to write static int i = 8; in the class? 回答1: Without inline , it's explicitly stated as only a declaration. As specified in [class.static.data]/2 The declaration of a non-inline static data member in its class definition is not a definition and may be of an incomplete type other than cv void.

Why is “inline” required on static inline variables?

梦想与她 提交于 2020-07-17 09:38:30
问题 C++17 allows static member variables to be defined thus: class X { public: static inline int i = 8; }; What is the rationale behind requiring the inline specification? Why not simply allow programmers to write static int i = 8; in the class? 回答1: Without inline , it's explicitly stated as only a declaration. As specified in [class.static.data]/2 The declaration of a non-inline static data member in its class definition is not a definition and may be of an incomplete type other than cv void.

Are all constexpr variable implicitly inline?

痴心易碎 提交于 2020-07-17 05:51:53
问题 I was playing around with auto template parameters and I was surprised that this code didn't compiled: constexpr auto bar = 2; template<auto& T> struct Foo { auto operator()() const { return T; } }; int main() { Foo<bar> b; b(); } Visual Studio 15.7 (preview 4) spit out these errors: error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument note: see declaration of 'Foo' note: see declaration of 'bar' error

Are all constexpr variable implicitly inline?

 ̄綄美尐妖づ 提交于 2020-07-17 05:51:51
问题 I was playing around with auto template parameters and I was surprised that this code didn't compiled: constexpr auto bar = 2; template<auto& T> struct Foo { auto operator()() const { return T; } }; int main() { Foo<bar> b; b(); } Visual Studio 15.7 (preview 4) spit out these errors: error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument note: see declaration of 'Foo' note: see declaration of 'bar' error

Target requires the language dialect “CXX17” (with compiler extensions), but CMake does not know the compile flags to use to enable it

落花浮王杯 提交于 2020-07-06 09:09:11
问题 So I've been trying to include the <filesystem> into my project, which seem to be a bigger problem than I thought. <filesystem> should be part of c++17, I need to add that definition into my CMakeList. My root CmakeLists look like this: MESSAGE(“In src CMAKELIST”) # # Build everything in include/ directory add_subdirectory(include) # #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $

Target requires the language dialect “CXX17” (with compiler extensions), but CMake does not know the compile flags to use to enable it

浪子不回头ぞ 提交于 2020-07-06 09:08:26
问题 So I've been trying to include the <filesystem> into my project, which seem to be a bigger problem than I thought. <filesystem> should be part of c++17, I need to add that definition into my CMakeList. My root CmakeLists look like this: MESSAGE(“In src CMAKELIST”) # # Build everything in include/ directory add_subdirectory(include) # #set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) #set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $

Why doesn't the compiler warn against ODR violations in the same translation unit

我们两清 提交于 2020-07-05 11:38:05
问题 In the same translation unit, ODR problems are easy to diagnose. Why then does the compiler not warn against ODR violations in the same translation unit? For example in the following code https://wandbox.org/permlink/I0iyGdyw9ynRgny6 (reproduced below), there is an ODR violation with detecting if std::tuple_size has been defined. And further the undefined behavior is evident when you uncomment the defintiions of three and four . The output of the program changes. Just trying to understand why