c++17

Template class with invalid member functions

限于喜欢 提交于 2019-12-06 04:28:28
Is it legal in C++ to have instantiate class templates with classes that do not work with some of its member functions? For example: class A { public: void f() { } }; class B { }; template<typename T> class Wrapper { private: T t_; public: void call_f() { t_.f(); } }; int main() { Wrapper<A> a; Wrapper<B> b; a.call_f(); } This code compiles, and I can use b , as long as I don't try to call b.call_f() . (Also explicitly instantiating it with template class Wrapper<B>; causes a compilation error because that instantiates all member functions.) Is this guaranteed to work or is it undefined

Is copy/move elision allowed to make a program using deleted functions well-formed?

空扰寡人 提交于 2019-12-06 04:18:54
问题 Consider the following code: #include <iostream> struct Thing { Thing(void) {std::cout << __PRETTY_FUNCTION__ << std::endl;} Thing(Thing const &) = delete; Thing(Thing &&) = delete; Thing & operator =(Thing const &) = delete; Thing & operator =(Thing &&) = delete; }; int main() { Thing thing{Thing{}}; } I expect Thing thing{Thing{}}; statement to mean construction of temporary object of Thing class using default constructor and construction of thing object of Thing class using move

Why does an std::any_cast of a passed std::any inside a dlopen'd function raise an error

纵然是瞬间 提交于 2019-12-06 03:42:35
I am toying around with c++17 and plugins, and I have run into an error that I cannot get around. In the following MWE I can call a local function that takes a std::any , and everything works as expected when I try to read the contents. When I load this exact same function through a plugin (dlopen), it correctly sees the type on the any, but it cannot std::any_cast the contents. Any help would be greatly appreciated in figuring out what is causing this error. Here is my environment, MWE, and resulting error. >> g++ --version g++ (GCC) 7.1.1 20170526 (Red Hat 7.1.1-2) Copyright (C) 2017 Free

How to represent a variadic templated type based on another group of variadic template arguments in modern C++?

丶灬走出姿态 提交于 2019-12-06 03:31:00
Suppose I have a following variadic template structure: template <class... T> struct Example {}; Now I want to define a template function: template<class... S> ??? f() { return Example<???> } where the specialization of Example<> is depend on the template parameter S of f . To be more concrete (and simple), now I just want to return Example<int, ...,int> , where the number of int is the size of the parameter pack S . How can it be done in modern C++, i.e. C++11/14/17? More generally, is there a way to at compile time define a function on template parameters? When doing template meta

Is std::move safe in an arguments list when the argument is forwarded, not move constructed?

纵然是瞬间 提交于 2019-12-06 02:46:35
问题 Trying to provide a solution to std::string_view and std::string in std::unordered_set, I'm playing around with replacing std::unordered_set<std::string> with std::unordered_map<std::string_view, std::unique_ptr<std::string>> (the value is std::unique_ptr<std::string> because the small string optimization would mean that the address of the string 's underlying data will not always be transferred as a result of std::move . My original test code, that seems to work, is (omitting headers): using

Does std::allocator handle over-aligned types in C++17?

半城伤御伤魂 提交于 2019-12-06 02:42:09
问题 C++17 introduces std::aligned_alloc and alignment-aware new that can do over-aligned allocations, but what about std::allocator ? Does it handle over-aligned types? 回答1: In N4659(C++17 DIS), 23.10.9.1 [allocator.members], bullet 2 T* allocate(size_t n); Returns: A pointer to the initial element of an array of storage of size n * sizeof(T), aligned appropriately for objects of type T . Compared to C++14, the sentence It is implementation-defined whether over-aligned types are supported has

Folding a parameter pack of N types into N-1 pairs

好久不见. 提交于 2019-12-06 02:16:24
I am trying to fold a parameter pack of N different types into a std::tuple of N-1 std::pairs with respective types. So for example the expression ResolveToTupleOfPairs<void, int, long>::Type tuple; should evaluate to std::tuple<std::pair<void, int>, std::pair<int, long>> tuple; So I am searching for an implementation of the ResolveToTupleOfPairs type to fold the parameter pack as explained. My current implementation follows, but obviously it causes the type to be a tuple of pairs which each hold the same type twice instead of <T0, T1>, <T1, T2>, ... . template<typename... T> struct

Order of Evaluation for Fold Expressions

旧巷老猫 提交于 2019-12-06 01:35:37
Fold expressions seem to be a nice way to apply a function to each element of a tuple. However, if the applied function has side effects, the order of function invocations might be an important concern. Consider: #include <iostream> template<typename... Ts> void printStuff(Ts... args) { ( ([](auto&& v) { std::cout << v << " "; })(args), ... ); std::cout << '\n'; } int main() { printStuff("hello", 42, 1.5f); // expected output: hello 42 1.5 } This seems to work . But is the order of evaluation for the lambdas guaranteed here or could I end up with the values being flipped around in the output?

What is the correct way to implement iterator and const_iterator in C++17?

此生再无相见时 提交于 2019-12-06 01:28:35
问题 When implementing a custom container I came to the point where I needed to implement iterators. Of course I didn't want to write the code twice for const and non-const iterator. I found this question detailing a possible implementation like this: template<class T> class ContainerIterator { using pointer = T*; using reference = T&; ... }; template<class T> class Container { using iterator_type = ContainerIterator<T>; using const_iterator_type = ContainerIterator<const T>; } But I also found

Is it possible to get the first type of a parameter pack in a one-liner?

浪尽此生 提交于 2019-12-06 01:01:20
I have a parameter pack given in a variadic template class and want to extract the first type. Currently I do this, which works fine but is somehow cumbersome. Is it possible to do the same thing simpler? FirstEntityType should be defined to have the type of the first type in EntityTs . Note, I want to keep the signature of the class template. I know that it would be possible to write template<typename FirstEntityType, typename... OtherEntityTypes> , it is however something that I don't want to do. template<typename... EntityTs> class EntityContext { template<typename T, typename ... Ts>