c++11

Ternary operator + C++11 constructor from initializer_list

这一生的挚爱 提交于 2021-02-16 05:37:02
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried

Explicit template instantiation with variadic templates

£可爱£侵袭症+ 提交于 2021-02-16 04:48:39
问题 I have several template classes Impl (with some abstract methods) that are partially implemented in CPP files so that I need to explicitly instantiate my templates for the linker to find it, like this: template class Impl<T0>; template class Impl<T1>; template class Impl<T2>; ... template class Impl<Tx>; As the number of types Tx grows, I would like to find a better way than to manually expand these lists of explicit instantiations in all necessary files. I thought I could use variadic

Boost variant visitor with an extra parameter

烈酒焚心 提交于 2021-02-15 20:51:04
问题 I have code that resembles below. typedef uint32_t IntType; typedef IntType IntValue; typedef boost::variant<IntValue, std::string> MsgValue; MsgValue v; Instead of saying this, IntValue value = boost::apply_visitor(d_string_int_visitor(), v); I would like to pass an extra parameter like this: But operator() gives a compile error. //This gives an error since the overload below doesn't work. IntValue value = boost::apply_visitor(d_string_int_visitor(), v, anotherStr); class d_string_int

How to detect the presence and type of a member variable given its name?

吃可爱长大的小学妹 提交于 2021-02-15 11:59:28
问题 I know how to write a class that can detect at compile time if a given class T has a member with a given name with given type Type, e.g. #include <type_traits> template <typename T, typename Type, bool = std::is_class<T>::value> struct has_member_foo { private: template <Type T::*> struct helper; template <typename U> static std::false_type test(...); template <typename U> static std::true_type test(helper<&U::foo> *); typedef decltype(test<T>(nullptr)) testresult; public: static const bool

How to perfectly forward `auto&&` in a generic lambda?

允我心安 提交于 2021-02-15 10:16:34
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to

How to perfectly forward `auto&&` in a generic lambda?

不羁岁月 提交于 2021-02-15 10:16:25
问题 C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include <utility> void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward<auto>(v)); }(8); // error } How to perfectly forward auto&& in a generic lambda? 回答1: auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype ? [](auto&& v) { f(std::forward<decltype(v)>(v)); }(8); Scott Meyers has more details. 来源: https://stackoverflow.com/questions/24535430/how-to

uniform initialization with variadic templates

半城伤御伤魂 提交于 2021-02-15 06:05:15
问题 I have a POD ChParam and it's a parameter in the variadic template function set . I'd like to pass to function arguments(constructor parameters) in curly braces p.set({ Param::D, 1000.f }, { Param::p, 2000.f }) . And thought that the constructor will be called implicitly and the ChParam objects will be created. But it's impossible, I should explicitly create an object a.set(ChParam{ Param::D, 1000.f }, ChParam{ Param::p, 2000.f }); is it possible somehow to use the variant p.set({ Param::D,

How to design a template class with switchable member variables at compile-time in C++?

老子叫甜甜 提交于 2021-02-11 18:21:32
问题 | ■ Probelm definition____________________ I'm trying to design a super-flexible, but memory efficient module satisfying below properties. It can switch OFF unnecessary member variables depending on the situation. Which variables it will own are determined at the compile-time . I somehow made one which determines its member list " before " the compile-time , using macros and enumerator flags . see below : ▼ TraitSwitch.h #pragma once // Macros to switch-off source codes themselves. #define ON

How to design a template class with switchable member variables at compile-time in C++?

此生再无相见时 提交于 2021-02-11 18:21:08
问题 | ■ Probelm definition____________________ I'm trying to design a super-flexible, but memory efficient module satisfying below properties. It can switch OFF unnecessary member variables depending on the situation. Which variables it will own are determined at the compile-time . I somehow made one which determines its member list " before " the compile-time , using macros and enumerator flags . see below : ▼ TraitSwitch.h #pragma once // Macros to switch-off source codes themselves. #define ON

std::shared_ptr initialized with other shared_ptr data

浪子不回头ぞ 提交于 2021-02-11 18:15:54
问题 I'm using recently new C++11 features such as std::shared_ptr, because I am converting a C code into Classes and in this code there is an heavy usage of "old-fashioned" pointers. I have one doubt: in a method of a class, where i use local shared_ptr, can i initialized them with the data of the passed reference to another smart_ptr and then modify the data? Example: void myFunction(std::shared_ptr<T> &my_reference) { std::shared_ptr<T> my_local_ptr(my_reference.get()); /* Doing stuff on local