c++14

Black Magic using Initializer_list and pack expansion

谁说我不能喝 提交于 2021-01-22 14:35:52
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations

Black Magic using Initializer_list and pack expansion

牧云@^-^@ 提交于 2021-01-22 14:33:10
问题 To expand flexible function parameters, there is a method using std::initializer_list . However I couldn't understand it. Can anyone explain this in an understandable way? template<typename T, typename... Args> auto print(T value, Args... args) { std::cout << value << std::endl; return std::initializer_list<T>{([&] { std::cout << args << std::endl; }(), value)...}; } 回答1: This is a very confused way of doing things, but C++14 requires that we do something similar. I'll explain the limitations

Abstract class and unique pointer

一个人想着一个人 提交于 2021-01-21 03:48:48
问题 I have the following error in my code: error: allocating an object of abstract class type 'Material' I don't know how to handle this case. I'm aware that std::make_unique performs an allocation, so it can't allocate the object of type Material , but I don't know how to correct it. #include <iostream> #include <memory> struct Material { Material() = default; virtual int get_color() const = 0; }; struct Basic : public Material { Basic() = default; virtual int get_color() const override { return

Abstract class and unique pointer

自古美人都是妖i 提交于 2021-01-21 03:45:10
问题 I have the following error in my code: error: allocating an object of abstract class type 'Material' I don't know how to handle this case. I'm aware that std::make_unique performs an allocation, so it can't allocate the object of type Material , but I don't know how to correct it. #include <iostream> #include <memory> struct Material { Material() = default; virtual int get_color() const = 0; }; struct Basic : public Material { Basic() = default; virtual int get_color() const override { return

Validity and/or lifetime extension of mem-initializer in aggregate initialization

南笙酒味 提交于 2021-01-20 20:01:27
问题 CWG 1815 asked (with minor edits): struct A {}; struct B { A&& a = A{}; }; B b1; // #1 B b2{A{}}; // #2 B b3{}; // #3 [...] #2 is aggregate initialization, which binds B::a to the temporary in the initializer for b2 and thus extends its lifetime to that of b2 . #3 is aggregate initialization, but it is not clear whether the lifetime of the temporary in the non-static data member initializer for B::a should be lifetime-extended like #2 or not, like #1 . Per the Notes on that issue, at Issaquah

enable_if template param is lambda (with particular signature)

爷,独闯天下 提交于 2021-01-20 08:24:22
问题 I have something this: template<typename T> class Image { Image(int w, int h, T defaultVal){ for(int i=0; i<h; i++) for(int j=0; j<w; j++) pixel(j, i) = defaultVal; } template<typename F> Image(int w, int h, F initializer){ for(int i=0; i<h; i++) for(int j=0; j<w; j++) pixel(j, i) = initializer(j, i); } // ... }; My intention is to be able to instantiate an Image like this: Image<int> img0(w, h, 0); // image of zeroes Image<int> imgF(w, h, [](int j, int i){ // checkerboard image return (j/10

How to Write a Lambda Wrapping a Function with Optional Return Value

我只是一个虾纸丫 提交于 2021-01-05 11:46:45
问题 I have tried to write a lambda that measures the execution time of arbitrary functions. With a lot of help I have managed that for C++14 and functions having a return value, see Measure execution time of arbitrary functions with C++14 lambda. Then I wanted my code to also work with C++11, therefore I have implemented the same idea with template functions. Finally I have realized that this code does not work for functions having no return value. It has been quite simple to generalize the

How to avoid code duplicates with class template specializations

依然范特西╮ 提交于 2021-01-03 15:48:59
问题 I'd like to avoid the duplicates in the code below. #include <iostream> struct Bar{}; template <class... Args> struct FooClass; template <class... Args> inline void foo(Args&&... args) { FooClass<Args...>::impl(std::forward<Args>(args)...); } // Duplicate 1. // Const ref version template <> struct FooClass<Bar const&> { inline static void impl(const Bar& b) { std::cout << "dup1" << std::endl; } }; // Duplicate 2. // Copy version template <> struct FooClass<Bar> { inline static void impl(const

How to avoid code duplicates with class template specializations

让人想犯罪 __ 提交于 2021-01-03 15:48:09
问题 I'd like to avoid the duplicates in the code below. #include <iostream> struct Bar{}; template <class... Args> struct FooClass; template <class... Args> inline void foo(Args&&... args) { FooClass<Args...>::impl(std::forward<Args>(args)...); } // Duplicate 1. // Const ref version template <> struct FooClass<Bar const&> { inline static void impl(const Bar& b) { std::cout << "dup1" << std::endl; } }; // Duplicate 2. // Copy version template <> struct FooClass<Bar> { inline static void impl(const

How to make the current coding structure more flexible

那年仲夏 提交于 2021-01-01 09:38:29
问题 I need help with my current workflow , though everthing works as expected but i would like to make the structure more robust. I have a application where i change the data by string literal. WAVEFRONT*TREEVIEW*Main$Text*GEOMETRY*TEXT SET TEXT ABCD as shown in the image where "Main$Text" is the path of the data item the the tree structure. 1) First step-> i have a class to handle the command string and take appropriate action. int SumCommandInterface::receiveCommand(std::string stdtsrCommand ,