c++14

Range/Loop through N variables in [modern] C++

不打扰是莪最后的温柔 提交于 2019-12-06 06:48:34
What's a succinct way of ranging through N variables, of any type each, to perform an operation? Let's say I have variables a , b , c , d , e and want to go through all of them performing some operation. Use Boost.Hana and generic lambdas: #include <tuple> #include <iostream> #include <boost/hana.hpp> #include <boost/hana/ext/std/tuple.hpp> struct A {}; struct B {}; struct C {}; struct D {}; struct E {}; int main() { using namespace std; using boost::hana::for_each; A a; B b; C c; D d; E e; for_each(tie(a, b, c, d, e), [](auto &x) { cout << typeid(x).name() << endl; }); } http://coliru.stacked

Correct usage of `for_each_arg` - too much forwarding?

隐身守侯 提交于 2019-12-06 06:36:51
问题 I'm really happy to have discovered for_each_arg(...), which makes dealing with argument packs much easier. template<class F, class...Ts> F for_each_arg(F f, Ts&&...a) { return (void)initializer_list<int>{(ref(f)((Ts&&)a),0)...}, f; } I'm, however, confused on its correct usage. There are many arguments that need to be perfectly forwarded, but am I performing any unnecessary forwarding? Reading the code becomes harder with excessive fowarding. struct UselessContainer { // Expects a perfectly

Are these vector definitions “constant initialization”?

雨燕双飞 提交于 2019-12-06 06:22:21
This question is about the code (at namespace scope): std::vector<int> v1; std::vector<int> v2(4); In section 3.6.2 of C++14 (N4140) there is defined a term Constant initialization : Constant initialization is performed: [omitted - about reference initialization] if an object with static or thread storage duration is initialized by a constructor call, and if the initialization full-expression is a constant initializer for the object; if an object with static or thread storage duration is not initialized by a constructor call and if either the object is value-initialized or every full

Does unordered_map iterator/reference invalidation allow for cuckoo, hopscotch, and robin hood hashing?

人走茶凉 提交于 2019-12-06 05:35:49
问题 I'm trying to figure out if it's possible to build a conformant, efficient implementation of modern C++'s std::unordered_map using techniques like Cuckoo Hashing, Hopscotch Hashing, and Robin Hood Hashing that allow for very compact tables, high load factors, and maintain high performance. What's special about these techniques is that they involve potentially moving some elements around to make room for others, rather than just chaining, or probing until an open slot is found (as in linear or

Partial specilization of static variable template in class template

你说的曾经没有我的故事 提交于 2019-12-06 05:27:28
问题 If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77

Template specialisation with default argument [duplicate]

≯℡__Kan透↙ 提交于 2019-12-06 05:03:01
This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed. Why is it that the second specialization is chosen over the first since both of them effectively

Self-document a type-alias (typedef) to indicate that it will be used in another certain class

江枫思渺然 提交于 2019-12-06 04:52:15
How to self-document a type-alias that is used in another certain library? In the below example, class User defines an alias User::type that is supposed to be referred only in class Library via T::type . Here is the diagram :- Library.h Library<T> expected only T that defines a certain alias (e.g. T::type in this example). #include <iostream> class Base{}; //dummy for the sake of example template<class T>class Library{ Base* t=nullptr; public: typename T::type getValue(){return static_cast<typename T::type>(t);} //some complex function, e.g. T::aType::doSomething() }; In real cases, Library<T>

How to build Qt 5 from source with c++14 support

主宰稳场 提交于 2019-12-06 04:35:23
I am attempting to build Qt 5.4.1 pulled from git with the -std=c++14 flag for gcc4.9. But I am not sure about how to properly pass the flag into the build process. I have read that adding CONFIG += c++14 to a qt project file should work since Qt5.4, so I have added it into the qt.pro located in the top folder. But Qt is still compiled with -std=c++0x (c++11). If you using g++ or clang++ to build the Qt 5 , then go to qt5/qtbase/mkspecs/common/g++-base.conf or qt5/qtbase/mkspecs/common/clang.conf respectively and simply change the right hand side of QMAKE_CXXFLAGS_CXX11 = -std=c++11 assignment

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

gcc vs clang - ambiguous overload when using `make_overload` variadic lambda inheritance [duplicate]

ぐ巨炮叔叔 提交于 2019-12-06 02:55:07
This question already has an answer here: Overloaded lambdas in C++ and differences between clang and gcc 2 answers Time for another round of clang vs gcc . Live example on godbolt.org . Test 0 : overloaded callable object struct Trad { auto operator()(int) { return 1; } auto operator()(float) { return 2; } auto operator()(double) { return 3; } }; int main() { assert(Trad{}(1) == 1); assert(Trad{}(1.f) == 2); assert(Trad{}(1.0) == 3); } g++ 5.2 compiles and run. clang++ 3.5 (and later versions) compiles and run. Test 1 : overloaded callable object, generated via lambda inheritance template