c++14

Variadic templates and switch statement?

那年仲夏 提交于 2019-12-20 09:16:37
问题 I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments): template <typename T1, typename T2> bool func(int& counter, T1 x1, T2 x2) { switch (counter) { case 0: if (func2<T1>(x1)) { counter++; return true; } else { return false; } case 1: if (func2<T2>(x2)) { counter++; return true; } else { return false; } default: return true; } } I want to write this function

Is C++14 adding new keywords to C++?

巧了我就是萌 提交于 2019-12-20 08:24:02
问题 The C++ Standards Committee tends to shy away from adding new keywords to the language, yet with C++11 that was not the case. Some examples: constexpr decltype thread_local auto // New usage noexcept nullptr static_assert alignof alignas Are there any new keywords introduced with C++14? 回答1: Table 4 (Keywords) in N3936 (C++14): alignas continue friend register true alignof decltype goto reinterpret_cast try asm default if return typedef auto delete inline short typeid bool do int signed

Is C++14 adding new keywords to C++?

前提是你 提交于 2019-12-20 08:23:04
问题 The C++ Standards Committee tends to shy away from adding new keywords to the language, yet with C++11 that was not the case. Some examples: constexpr decltype thread_local auto // New usage noexcept nullptr static_assert alignof alignas Are there any new keywords introduced with C++14? 回答1: Table 4 (Keywords) in N3936 (C++14): alignas continue friend register true alignof decltype goto reinterpret_cast try asm default if return typedef auto delete inline short typeid bool do int signed

Is C++14 adding new keywords to C++?

白昼怎懂夜的黑 提交于 2019-12-20 08:22:05
问题 The C++ Standards Committee tends to shy away from adding new keywords to the language, yet with C++11 that was not the case. Some examples: constexpr decltype thread_local auto // New usage noexcept nullptr static_assert alignof alignas Are there any new keywords introduced with C++14? 回答1: Table 4 (Keywords) in N3936 (C++14): alignas continue friend register true alignof decltype goto reinterpret_cast try asm default if return typedef auto delete inline short typeid bool do int signed

Handling references to `expressions` in a `vector_binary_operation` class without unnecessary copies

心已入冬 提交于 2019-12-20 05:25:08
问题 I'm trying to implement a simple vector class which uses expression templates in order to avoid that expressions such as vector w = x + y + z are implemented inefficiently (the implementation would first produce a temporary vector to hold x + y and then produce another vector with the elements of z added in): namespace math { template<class E> class expression { public: auto size() const { return static_cast<E const&>(*this).size(); } auto operator[](std::size_t i) const { if (i >= size())

Indirect forwarding references

女生的网名这么多〃 提交于 2019-12-20 04:28:06
问题 It is well known that "direct" forwarding references works in an easy way : template<typename T> void f(T &&t); // Here we are. Now, how to use forwarding references in an indirect way : template<typename T> void f(some_class_template<T> &&f); // Here it is an rvalue reference and not universal one Is there a way to have a forwarding reference in this second case? 回答1: No, this is not possible. If you want to constrain the function to only accept some_class_template , you have to use a type

How does placement new know which layout to create?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 04:07:11
问题 #include <iostream> #include <typeinfo> struct A { int a; }; struct B : virtual A { int b; }; struct C : virtual A { int c; }; struct D : B,C { int d; }; int main() { D complete; B contiguous; B & separate = complete; B * p[2] = {&separate, &contiguous}; // two possible layouts for B: std::cout<< (int)((char*)(void*) &p[0]->a -(char*)(void*)&p[0]->b)<<" "<< sizeof(*p[0])<< "\n"; std::cout<< (int)((char*)(void*) &p[1]->a -(char*)(void*)&p[1]->b)<<" "<< sizeof(*p[1])<< "\n"; alignas(B) char

Variable already defined in .obj; What is going on here?

你离开我真会死。 提交于 2019-12-20 02:32:08
问题 head.h #pragma once namespace foo { int bar; int funct1(); } head.cpp #include "head.h" int foo::funct1() { return bar; } main.cpp #include <iostream> #include "head.h" int main() { foo::bar = 1; std::cout << foo::funct1() << std::endl; return 0; } Error LNK2005 "int foo::bar" (?bar@foo@@3HA) already defined in head.obj I don't understand what is going on. I tried looking for the answer but everyone's questions are so specific to their code and don't even look close to the problem that I am

Use of auto as return and parameters type in C++14

天大地大妈咪最大 提交于 2019-12-20 02:30:12
问题 In the 4th edition of Bjarne Stroustrup book (The C++ programing language) we read that: Using auto , we avoid redundancy and writing long type names. This is especially important in generic programming where the exact type of an object can be hard for the programmer to know and the type names can be quite long (§4.5.1). So, to understand the importance of this type. I made this small test program: #include <iostream> /*-----------------------------*/ auto multiplication(auto a, auto b) {

Ill-Formed, No Diagnostic Required (NDR): ConstExpr Function Throw in C++14

心已入冬 提交于 2019-12-20 02:28:25
问题 #include <iostream> using namespace std; constexpr int f(bool b){ return b ? throw 0 : 0; } // OK constexpr int f() { return f(true); } // Ill-Formed, No Diagnostic Required int main(){ try{ f(); }catch( int x ){ cout << "x = " << x << endl; } return 0; } This code is an example from the C++14 Standard (ISO/IEC 14882:2014), Section 7.1.5, Paragraph 5: For a non-template, non-defaulted constexpr function or a non-template, non-defaulted, non-inheriting constexpr constructor, if no argument