c++17

Is it a good practice to return the r-value reference from the r-value ref-qualified method?

这一生的挚爱 提交于 2019-12-11 03:02:50
问题 As I can see the general rule is not to return r-value references from functions at all (except for rare special cases). But what about class methods? There is an example in the C++ standard library of returning r-value reference from the r-value ref-qualified method of the class (std::optional<T>::operator*() and std::optional<T>::value() methods of the std::optional<T> class). See sections 23.6.3 Class template optional [optional.optional] and 23.6.3.5 Observers [optional.observe] of the C+

What C++17 standard say about calling delete on nullptr?

牧云@^-^@ 提交于 2019-12-11 02:25:40
问题 C++03 Standard say's: 5.3.5 Delete [...] In either alternative, if the value of the operand of delete is the null pointer the operation has no effect.[...] char *p = nullptr; delete p; //no effect It means, it is valid to delete null pointer in c++. What C++17 standard say about calling delete on nullptr pointer? 回答1: Yes it is valid, and it results in a noop. reference If expression evaluates to a null pointer value, no destructors are called, and the deallocation function is not called. 回答2

Virtual functions and std::function?

房东的猫 提交于 2019-12-11 02:13:01
问题 Consider the following code in C++17: #include <iostream> #include <functional> struct base { base() {std::cout << "base::base" << std::endl;} virtual ~base() {std::cout << "base::~base" << std::endl;} virtual void operator()() {std::cout << "base::operator()" << std::endl;} }; struct derived1: base { derived1() {std::cout << "derived1::derived1" << std::endl;} virtual ~derived1() {std::cout << "derived1::~derived1" << std::endl;} virtual void operator()() {std::cout << "derived1::operator()"

How to find maximum value in parameter pack?

↘锁芯ラ 提交于 2019-12-11 01:54:26
问题 Here is an example of problem: a constant variable's template should expand its type based on parameters. While direct way is possible, by giving size of type, or underlying typename, it's error-prone. #include <iostream> template<size_t bit> constexpr const uint16_t BIT = 1 << bit; template<size_t... bits> constexpr const uint16_t BITS = (uint16_t(1 << bits)|...); int main() { std::cout << BITS<0,1,3,12> << std::endl; } Idea is to implement template data type which would return type which is

Filter a tuple of types in c++17

廉价感情. 提交于 2019-12-11 01:48:26
问题 std::tuple a{1,3,4,5} -> make it to numbers greater than 3 std::tuple b{4,5} Or std::tuple a{ std::integral_constant<int,1> {}, std::integral_constant<int,3> {}, std::integral_constant<int,4> {}, std::integral_constant<int,5> {} } to std::tuple a{ std::integral_constant<int,4>{}, std::integral_constant<int,5>{} }; How to convert this at compile time? I can do this using integer_sequence but that is a cumbersome. Is there a simpler way in C++17 using fold expressions or std::apply Also after

Template argument deduction failed with default template parameter

只谈情不闲聊 提交于 2019-12-11 01:47:00
问题 #include <cstdint> #include <iostream> class MyBar { public: void print() { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; template <class Bar = MyBar> class Foo{ public: Foo(const char* name, const uint32_t i) { Bar b; b.print(); } }; int main(int argc, char** argv) { auto pFoo1 = new Foo("abc", 3); } Compiler gave me: template_ctor.cpp: In function ‘int main(int, char**)’: template_ctor.cpp:21:31: error: class template argument deduction failed: auto pFoo1 = new Foo("abc", 3); ^

What are the correct memory orders to use when inserting a node at the beginning of a lock free singly linked list?

混江龙づ霸主 提交于 2019-12-11 01:40:23
问题 I have a simple linked list. There is no danger of the ABA problem, I'm happy with Blocking category and I don't care if my list is FIFO, LIFO or randomized. At long as the inserting succeeds without making others fails. The code for that looks something like this: class Class { std::atomic<Node*> m_list; ... }; void Class::add(Node* node) { node->next = m_list.load(std::memory_order_acquire); while (!m_list.compare_exchange_weak(node->next, node, std::memory_order_acq_rel, std::memory_order

How to copy an element of std::variant to a variable of another variant-type

不想你离开。 提交于 2019-12-10 23:50:27
问题 This is a followup on this answer. Assume we have two types of std:variant with partly the same member types. For instance if we have struct Monday {}; struct Tuesday {}; /* ... etc. */ using WeekDay= std::variant<Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday>; using Working_Day= std::variant<Monday, Tuesday, Wednesday, Thursday, Friday>; Working_Day is a sub-type of WeekDay . Now how can we copy a variable of one type to a variable of the other type? If all type members of

Am I using std::move() too often?

烈酒焚心 提交于 2019-12-10 23:03:31
问题 I finally feel like I understand move semantics in Modern C++, and it's had a dramatic change on the way I write code. Right now, I'm working on an application that uses dependency injection and I'm incorporating my newfound knowledge of move semantics, but I end up using std::move() so much that I'm worried I'm using it incorrectly. Previously, if I wanted to inject a dependency that I needed a copy of in my object, I'd write my constructor like this: class NeedsCopyOfFoo { public:

How do I re-assign a function name in C++

自闭症网瘾萝莉.ら 提交于 2019-12-10 21:52:01
问题 I have a problem in C++ that is similar to this example problem. In this case I have two member-functions that have an identical interface. Based on the information in a string passed to the super function, I would like to assign one of the two member functions to the variable class_func . Is there a way to do this? // test.hpp class TestClass { public: double master_function(double a, double b, std::string func_name); private: double add(double a, double b); double subtract(double a, double