c++17

Can an inline variable be changed after initialization in C++17?

巧了我就是萌 提交于 2020-01-03 07:19:18
问题 My scenario is the following (it worked in clang but not in gcc) liba.hpp: inline int MY_GLOBAL = 0; libother.cpp: (dll) #include "myliba.hpp" void myFunc() { // MYGLOBAL = 28; } someexe.cpp: RunAppThatUsesBothLibAandLibOther(); The problem is that the inline variable was showing 0 in places where I expected 28 because it was alrady modified at run-time. MSVC disagrees with this, but clang does the thing I would expect. The question is: can inline variables be modified at run-time in my

How to static cast throwing function pointer to noexcept in C++17?

杀马特。学长 韩版系。学妹 提交于 2020-01-03 06:59:07
问题 C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers. void (*ptr_to_noexcept)() noexcept = nullptr; void (*ptr_to_throwing)() = ptr_to_noexcept; // implicit conversion http://eel.is/c++draft/expr.static.cast#7 says that static_cast can perform the inverse of such a conversion. void (*noexcept_again)() noexcept = static_cast<void(*)() noexcept>(ptr_to_throwing); Unfortunately, both GCC and

How to use std::min_element in C++17?

流过昼夜 提交于 2020-01-03 06:38:06
问题 I have small piece of code to print smallest element in the range using std::min_element. cppreference example print the index of smallest element, but i want to print smallest element instead of index number. #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> v{3, 1, 4, 1, -5, 9}; std::cout << std::min_element(std::begin(v), std::end(v)); } But, I got following an error: main.cpp: In function 'int main()': main.cpp:8:15: error: no match for 'operator<<'

C++17 almost uniform initialization

南笙酒味 提交于 2020-01-03 05:23:09
问题 At the end of this video (starting at 15:57) there is advice on how to use almost uniform initialization in C++17: video here The gist goes like this: use always direct initialization auto a{...}; and MyType a{...}; Do not use copy initialization = {...} for your types. #include <iostream> struct MyType { explicit MyType(std::initializer_list<int>) { std::cout << "Called std::initializer_list<int>" << std::endl; } explicit MyType(int) { std::cout << "Called int." << std::endl; } MyType(int,

Ensure that char pointers always point to the same string literal

依然范特西╮ 提交于 2020-01-02 23:13:13
问题 Given the code // somewhere in the program const char* p1 = "Hello World"; // somewhere else in the program const char* p2 = "Hello World"; is there a way to ensure that p1 == p2 is always satisfied within the entire program / library? By that I mean that p1 and p2 always refer to the same string literal. The Reason behind it What I'm trying to achieve is to use const char* as a key for std::map<const char*, something> . I have a macro #define nameof(id) #id that mimics the behavior of the

Ensure that char pointers always point to the same string literal

Deadly 提交于 2020-01-02 23:12:51
问题 Given the code // somewhere in the program const char* p1 = "Hello World"; // somewhere else in the program const char* p2 = "Hello World"; is there a way to ensure that p1 == p2 is always satisfied within the entire program / library? By that I mean that p1 and p2 always refer to the same string literal. The Reason behind it What I'm trying to achieve is to use const char* as a key for std::map<const char*, something> . I have a macro #define nameof(id) #id that mimics the behavior of the

How to implement std::when_any without polling?

元气小坏坏 提交于 2020-01-02 19:30:14
问题 Consider http://en.cppreference.com/w/cpp/experimental/when_any. The following is just a naive and simplified implementation: #include <future> template<typename Iterator> auto when_any(Iterator first, Iterator last) { while (true) { for (auto pos = first; pos != last; ++pos) { if (pos->is_ready()) { return std::move(*pos); } } } } I am not satisfied because it is a busy polling in an infinite loop. Is there a way to avoid busy polling? 回答1: A polling free version would launch 1 thread per

Writing a Factory method for STL random number generators

狂风中的少年 提交于 2020-01-02 16:54:34
问题 I'm trying to provide an interface — through a config file — for my users to choose a distribution for some of the parameters that they are using. I would like to use STL random number generator algorithms for this purpose. Let's assume that my program reads a JSON from a command line. For the JSON provided below, the program needs to realize that it should generate a random number from the normal distribution with given mean and standard variation. (I'm using the same parameter names as STL

Compile error when std::filesystem header file is added to my program

给你一囗甜甜゛ 提交于 2020-01-02 09:57:16
问题 I am trying to compile a simple C++ program with std::filesytem header file included! #include <iostream> #include <filesystem> int main() { std::cout << "Hello, World!" << std::endl; return 0; } On compiling I get the following error In file included from C:/PROGRA~1/MINGW-~1/X86_64~2.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37, from C:\Users\{User}\CLionProjects\untitled3\main.cpp:2: C:/PROGRA~1/MINGW-~1/X86_64~2.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0

Compile error when std::filesystem header file is added to my program

允我心安 提交于 2020-01-02 09:57:12
问题 I am trying to compile a simple C++ program with std::filesytem header file included! #include <iostream> #include <filesystem> int main() { std::cout << "Hello, World!" << std::endl; return 0; } On compiling I get the following error In file included from C:/PROGRA~1/MINGW-~1/X86_64~2.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37, from C:\Users\{User}\CLionProjects\untitled3\main.cpp:2: C:/PROGRA~1/MINGW-~1/X86_64~2.0-W/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0