c++11

Is it possible in C++11 to combine functions into a new function?

痴心易碎 提交于 2021-02-20 18:51:54
问题 This is more a kind of theoretical question. Is it possible in C++11 to combine functions into a new function? For example : auto f = [](int i){return i * 2;}; auto g = [](int i){return i + 10;}; So this works: auto c = f(g(20)); // = 60 But I want an object that stores the combination, like auto c = f(g); std::cout << c(20) << std::endl; //prints 60 Edit: Additionally what i want to create is a function a, which you can give a function b and an int n , and which returns the n'th combination

GCC throws init-list-lifetime warning on potentially valid code?

守給你的承諾、 提交于 2021-02-20 10:16:29
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

GCC throws init-list-lifetime warning on potentially valid code?

故事扮演 提交于 2021-02-20 10:15:59
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

GCC throws init-list-lifetime warning on potentially valid code?

倾然丶 夕夏残阳落幕 提交于 2021-02-20 10:15:10
问题 I'm running on Debian unstable with GCC 9.3.0. There was a recent change on a project I work on that introduced code similar to what's below. #include <initializer_list> #include <map> #include <vector> std::map<int, std::vector<int>> ex = []{ /* for reused lists */ std::initializer_list<int> module_options; return (decltype(ex)) { {1, module_options = { 1, 2, 3 }}, {2, module_options}, }; }(); The idea is that identical subsections of the initializer lists are first declared at the top,

How does using ellipses for SFINAE work?

空扰寡人 提交于 2021-02-20 08:59:25
问题 When using SFINAE to select constructor overloads in the past, I have typically used the following: template <typename T> class Class { public: template <typename U = T, typename std::enable_if<std::is_void<U>::value, int>::type=0> Class() { std::cout << "void" << std::endl; } template <typename U = T, typename std::enable_if<!std::is_void<U>::value, int>::type=0> Class() { std::cout << "not void" << std::endl; } }; However, I just came across this alternative: template <typename U = T,

Namespace alias in c++

二次信任 提交于 2021-02-20 06:26:16
问题 I use c++11 while I need some classes from c++17 library. When using boost from which classes were added I wish to do the following: #if __cplusplus < CPP17 using std::any = boost::any; #endif Such alias is not allowed. Also extending the std namespace causes undefined behaviour. I wish my code to look the same regardles of the c++ version. Is there a clear way? 回答1: The clear way is to add a customized name for it. #if __cplusplus < CPP17 using my_any = boost::any; #else using my_any = std:

Namespace alias in c++

自作多情 提交于 2021-02-20 06:20:06
问题 I use c++11 while I need some classes from c++17 library. When using boost from which classes were added I wish to do the following: #if __cplusplus < CPP17 using std::any = boost::any; #endif Such alias is not allowed. Also extending the std namespace causes undefined behaviour. I wish my code to look the same regardles of the c++ version. Is there a clear way? 回答1: The clear way is to add a customized name for it. #if __cplusplus < CPP17 using my_any = boost::any; #else using my_any = std:

Boost Linking problems

微笑、不失礼 提交于 2021-02-20 05:12:41
问题 I have tried to link boost in my CMakeList file by set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_NO_SYSTEM_PATHS TRUE) set(Boost_INCLUDE_DIR /user/software/boost/boost-1.69-x86_64-cc7/include) set(Boost_LIBRARY_DIR /user/software/boost/boost-1.69-x86_64-cc7/lib) find_package(Boost 1.54 REQUIRED COMPONENTS filesystem program_options system) and i get the following warning when doing cmake CMake Warning at /user/software/cmake/cmake-3

Boost Linking problems

泄露秘密 提交于 2021-02-20 05:10:49
问题 I have tried to link boost in my CMakeList file by set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_NO_SYSTEM_PATHS TRUE) set(Boost_INCLUDE_DIR /user/software/boost/boost-1.69-x86_64-cc7/include) set(Boost_LIBRARY_DIR /user/software/boost/boost-1.69-x86_64-cc7/lib) find_package(Boost 1.54 REQUIRED COMPONENTS filesystem program_options system) and i get the following warning when doing cmake CMake Warning at /user/software/cmake/cmake-3

Boost Linking problems

守給你的承諾、 提交于 2021-02-20 05:10:48
问题 I have tried to link boost in my CMakeList file by set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_NO_SYSTEM_PATHS TRUE) set(Boost_INCLUDE_DIR /user/software/boost/boost-1.69-x86_64-cc7/include) set(Boost_LIBRARY_DIR /user/software/boost/boost-1.69-x86_64-cc7/lib) find_package(Boost 1.54 REQUIRED COMPONENTS filesystem program_options system) and i get the following warning when doing cmake CMake Warning at /user/software/cmake/cmake-3