c++14

What is the correct way to initialize static data members in C++ (98, 11 and 14)

谁都会走 提交于 2019-12-18 11:02:46
问题 What is the right way to initialize static data members in C++? I'm also interested in how it has changed from C++98, to C++11 to C++14. Here is an example: // bufferedOutput.h class BufferedOutput { // Static member declaration. static long bytecount; }; // bufferedOutput.cpp long BufferedOutput::bytecount = 50; Are there other ways to initialize static data members? 回答1: The rules have always been as follows: A const static data member (SDM) of integral or enumeration type can be

Guidelines to do constexpr operator-overloading?

馋奶兔 提交于 2019-12-18 10:50:09
问题 Consider a simple int Wrapper class with overloaded multiplication operator*= and operator* . For "old-style" operator-overloading, one can define operator* in terms of operator*= , and there are even libraries like Boost.Operators and its modern incarnation df.operators by @DanielFrey that reduce the boilerplate for you. However, for compile-time computations using the new C++11 constexpr , this convenience disappears. A constexpr operator* cannot call operator*= because the latter modifies

Iterating over different types

孤街浪徒 提交于 2019-12-18 10:01:49
问题 Given the following code: struct Window{ void show(); //stuff }w1, w2, w3; struct Widget{ void show(); //stuff }w4, w5, w6; struct Toolbar{ void show(); //stuff }t1, t2, t3; I want to show a bunch of items: for (auto &obj : {w3, w4, w5, t1}) obj.show(); However this does not compile since the std::initializer_list<T> in the for -loop cannot deduce T and in fact there is not really a T that would fit. I don't want to create a type erasure type because of the amount of code required and the

Can I generate a function without providing arguments?

ぐ巨炮叔叔 提交于 2019-12-18 09:36:15
问题 So c++17 has std::function Deduction Guides so given: int foo(); I can do: std::function bar(foo); But I'm stuck on a c++14 compiler. There I have to do something more like: function<int()> bar(foo) . I was wondering if there was a way to create a std::function without passing the function pointer and explicitly providing the function signature? So for example make_pair will deduce the type of it's return from it's arguments. I was wondering if I could write something similar for function s

pack fold expression (c++17 extension) available when building with c++14

不问归期 提交于 2019-12-18 09:08:50
问题 The following code contains a fold expression, which afaiu is a c++17 feature: template <typename... T> static bool variable_length_or(const T ... v) { return (v || ...); } bool foo () { return variable_length_or(true, false, true, false); } what I find odd is that both g++ and clang++ seem to be fine with it when building with -std=c++14 (compiler-explorer). They do create a warning: <source>:2:16: warning: pack fold expression is a C++17 extension [-Wc++17-extensions] return (v || ...);

Attributes from Boost.Spirit grammar: error from std:vector of boost::variant

陌路散爱 提交于 2019-12-18 08:48:23
问题 I got a working parser for reading position descriptions for a board game (international draughts, official grammar): #include <boost/spirit/home/x3.hpp> #include <iostream> namespace x3 = boost::spirit::x3; auto const colon = x3::lit(':'); auto const comma = x3::lit(','); auto const dash = x3::lit('-'); auto const dot = x3::lit('.'); auto const king = x3::char_('K'); auto const color = x3::char_("BW"); auto const num_sq = x3::int_; auto const num_pc = -king >> num_sq; // Kxx means king on

Is there an equivalent to the range-based `enumerate` loop from python in modern C++?

送分小仙女□ 提交于 2019-12-18 08:32:42
问题 Is there an equivalent to the range-based enumerate loop from python in C++? I would imagine something like this. enumerateLoop (auto counter, auto el, container) { charges.at(counter) = el[0]; aa.at(counter) = el[1]; } Can this be done with templates or macros? I'm aware that I can just use an old school for-loop and iterate until I reach container.size() . But I'm interested how this would be solved using templates or macros. EDIT I played a bit with boost iterators after the hint in the

Unqualified name lookup: Why local declaration hides declaration from using directive

a 夏天 提交于 2019-12-18 08:31:22
问题 Consider this code: namespace A { int i = 24; } namespace B { using namespace A; int i = 11; int k = i; // finds B::i, no ambiguity } And basic.lookup.unqual.2: §6.4.1 Unqualified name lookup [basic.lookup.unqual] The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace

Does RVO work on object members?

南楼画角 提交于 2019-12-18 07:35:09
问题 Consider the following: struct A { /* ... */ }; A foo() { auto p = std::make_pair(A{}, 2); // ... do something return p.first; } auto a = foo(); Will p.first be copied, moved or RVO-ed? 回答1: I've found in Visual Studio 2010 and in gcc-5.1 RVO is not applied (see for example http://coliru.stacked-crooked.com/a/17666dd9e532da76). The relevant section of the standard is 12.8.31.1 [class.copy]. It states that copy elision is permitted (my highlighting): in a return statement in a function with a

Is there any way of detecting arbitrary template classes that mix types and non-types?

陌路散爱 提交于 2019-12-18 06:48:06
问题 Is there any way of detecting whether a class is a normal type or is a template type (meta type) which may include non-type parameters ? I came up with this solution: #include <iostream> template <template<class...> class> constexpr bool is_template() { return true; } template <class> constexpr bool is_template() { return false; } struct Foo{}; template<class> struct TemplateFoo{}; template<class, int> struct MixedFoo{}; int main() { std::cout << std::boolalpha; std::cout << is_template<Foo>(