c++14

Create a type list combination of types in C++

眉间皱痕 提交于 2019-12-18 05:44:18
问题 Im trying to create some tool to create a list of types based on combinations of other types. Lets say we have three types struct A{}; struct B{}; struct C{}; I want to get a list of tuples which has every possible combination of N types A,B or C. For a N=2 case, this would be std::tuple<A,A> std::tuple<A,B> std::tuple<A,C> std::tuple<B,A> std::tuple<B,B> std::tuple<B,C> std::tuple<C,A> std::tuple<C,B> std::tuple<C,C> The idea is to create a tuple which holds a container for all those types,

Is it direct-initialization or copy-initialization?

二次信任 提交于 2019-12-18 05:24:07
问题 Initializing objects (instances of classes or structs) in C++ can be done in various ways. Some syntaxes evoke a direct-initialization of your object, other syntaxes lead to a copy-initialization . With copy-elision enabled in the compiler, both have identical performance. With copy-elision disabled, there is an additional copy/move constructor call upon every instantiation when you choose for the latter (copy-initialization). Conclusion: copy-initialization can have a performance-penalty!

Can reinterpret_cast (or any cast) convert xvalues to lvalues?

让人想犯罪 __ 提交于 2019-12-18 04:12:37
问题 Is the following code legal (by C++11 and/or C++14 standard(s))? #include <iostream> #include <utility> using namespace std; void foo(int &a) { cout << a << endl; } int main() { foo(reinterpret_cast<int &>(move(5))); } If yes, is it undefined behavior? If it's not undefined behavior, can I even mutate a inside foo without it becoming UB? It compiles on clang 3.5, not on gcc 4.9. GCC error: ➤ g++-4.9 -std=c++1y sample.cpp -o sample sample.cpp: In function 'int main()': sample.cpp:11:40: error:

Can we refer to member variables in a noexcept specification?

旧城冷巷雨未停 提交于 2019-12-18 04:07:10
问题 Please consider the following code snippet: template<class Tuple> class vector { public: typename Tuple::size_type size() const noexcept(noexcept(m_elements.size())) { return m_elements.size(); } private: Tuple m_elements; }; class tuple { public: using size_type = std::size_t; size_type size() const { return 0; } size_type size() noexcept { return 0; } }; int main() { vector<tuple> x; static_assert(noexcept(x.size()), "x.size() might throw"); return 0; } Is the use of the member variable m

Optimization of raw new[]/delete[] vs std::vector

心不动则不痛 提交于 2019-12-18 03:32:15
问题 Let's mess around with very basic dynamically allocated memory. We take a vector of 3, set its elements and return the sum of the vector. In the first test case I used a raw pointer with new[] / delete[] . In the second I used std::vector : #include <vector> int main() { //int *v = new int[3]; // (1) auto v = std::vector<int>(3); // (2) for (int i = 0; i < 3; ++i) v[i] = i + 1; int s = 0; for (int i = 0; i < 3; ++i) s += v[i]; //delete[] v; // (1) return s; } Assembly of (1) ( new[] / delete[

Optimization of raw new[]/delete[] vs std::vector

微笑、不失礼 提交于 2019-12-18 03:32:13
问题 Let's mess around with very basic dynamically allocated memory. We take a vector of 3, set its elements and return the sum of the vector. In the first test case I used a raw pointer with new[] / delete[] . In the second I used std::vector : #include <vector> int main() { //int *v = new int[3]; // (1) auto v = std::vector<int>(3); // (2) for (int i = 0; i < 3; ++i) v[i] = i + 1; int s = 0; for (int i = 0; i < 3; ++i) s += v[i]; //delete[] v; // (1) return s; } Assembly of (1) ( new[] / delete[

Enabling `-std=c++14` flag in Code::Blocks

北城余情 提交于 2019-12-17 23:24:00
问题 I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14 . How do I update the compiler and enable -std=c++14 flag for Code::Blocks? 回答1: To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features. Here’s how you can do it on Windows: Download MinGW from here

g++ 4.9 rejects valid aggregate initialization in C++14

余生长醉 提交于 2019-12-17 19:53:22
问题 Consider this code: struct S { int x; double y = 1.1; }; int main() { S s = {0}; } According to the C++14 standard, § 8.5.1/7 If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal- initializer, from an empty initializer list (8.5.4). the code should be perfectly valid. However, g++ 4.9.2 rejects the code (compiled with

SFINAE with C++14 return type deduction

你说的曾经没有我的故事 提交于 2019-12-17 19:44:26
问题 Thanks to C++14, we'll soon be able to curtail verbose trailing return types; such as the generic min example from David Abrahams 2011 post: template <typename T, typename U> auto min(T x, U y) -> typename std::remove_reference< decltype(x < y ? x : y) >::type { return x < y ? x : y; } Under C++14 the return type can be omitted, and min can be written as: template <typename T, typename U> auto min(T x, U y) { return x < y ? x : y; } This is a simple example, however return type deduction is

Constructors : difference between defaulting and delegating a parameter

给你一囗甜甜゛ 提交于 2019-12-17 18:57:07
问题 Today, I stumbled upon these standard declarations of std::vector constructors : // until C++14 explicit vector( const Allocator& alloc = Allocator() ); // since C++14 vector() : vector( Allocator() ) {} explicit vector( const Allocator& alloc ); This change can be seen in most of standard containers. A slightly different exemple is std::set : // until C++14 explicit set( const Compare& comp = Compare(), const Allocator& alloc = Allocator() ); // since C++14 set() : set( Compare() ) {}