c++14

Does attribute specifier sequence inherit?

烂漫一生 提交于 2019-12-17 18:35:23
问题 Look at this snippet: struct [[nodiscard]] Result { }; struct DiscardableResult: Result { }; Does DiscardableResult have the [[nodiscard]] attribute? If yes, is it possible to remove it somehow? 回答1: [dcl.attr.nodiscard]/2 says: A nodiscard call is a function call expression that calls a function previously declared nodiscard , or whose return type is a possibly cv-qualified class or enumeration type marked nodiscard . The return type of the function is DiscardableResult . This type is not

Does C++11, 14, 17 or 20 introduce a standard constant for pi?

青春壹個敷衍的年華 提交于 2019-12-17 17:59:26
问题 There is a rather silly problem with the number pi in C and C++. As far as I know M_PI defined in math.h is not required by any standard. New C++ standards introduced a lot of complicated math in the standard library - hyperbolic functions, std::hermite and std::cyl_bessel_i , different random number generators and so on and so forth. Did any of the 'new' standards bring in a constant for pi? If not - why? How does all this complicated math work without it? I am aware of similar questions

Why no emplacement iterators in C++11 or C++14?

萝らか妹 提交于 2019-12-17 17:45:13
问题 C++98 has front_inserter , back_inserter , and inserter , but there don't seem to be any emplacement versions of these in C++11 or draft C++14. Is there any technical reason we couldn't have front_emplacer , back_emplacer , and emplacer ? 回答1: Is there any technical reason we couldn't have front_emplacer, back_emplacer, and emplacer? No, there is no technical reason. As proof, here is a complete implementation of back_emplacer with a demo of your Use Case 1... #include <iterator> #include

Can we omit the double-braces for std::array in C++14?

痞子三分冷 提交于 2019-12-17 16:45:14
问题 I'm reading through the draft standard for C++14 right now, and perhaps my legalese is a bit rusty, but I can't find any mention of allowing initializations like the following std::array<int, 3> arr{1,2,3}; being legal. (EDIT: Apparently the above is legal syntax in C++11.) Currently in C++11 we have to initialize std::array as std::array<int, 3> arr{{1,2,3}}; // uniform initialization + aggregate initialization or std::array<int, 3> arr = {1,2,3}; I thought I heard somewhere that they were

c++11 variadic programming, how to define a tower of vectors

醉酒当歌 提交于 2019-12-17 16:34:25
问题 How (if possible) can I use c++11 variadic programming to define a series of vector 's in a function body, (or in other words, a sequence of N -dimensional arrays with decreasing N 's until 0), like the variables below? vector<vector<vector<int>>> v<3>; vector<vector<int>> v<2>; vector<int> v<1>; int v<0>; What I imagined is something like: #include <iostream> #include <vector> using namespace std; template<int ...> struct seq {}; template<int N, int ...S> struct gens : gens<N-1, N-1, S...> {

Why is `make_unique<T[N]>` disallowed?

有些话、适合烂在心里 提交于 2019-12-17 15:54:34
问题 Assume namespace std throughout. The C++14 committee draft N3690 defines std::make_unique thus: [n3690: 20.9.1.4]: unique_ptr creation [unique.ptr.create] template <class T, class... Args> unique_ptr<T> make_unique(Args&&... args); 1 Remarks: This function shall not participate in overload resolution unless T is not an array. 2 Returns: unique_ptr<T>(new T(std::forward<Args>(args)...)). template <class T> unique_ptr<T> make_unique(size_t n); 3 Remarks: This function shall not participate in

Can I obtain C++ type names in a constexpr way?

醉酒当歌 提交于 2019-12-17 15:53:47
问题 I would like to use the name of a type at compile time. For example, suppose I've written: constexpr size_t my_strlen(const char* s) { const char* cp = s; while(*cp != '\0') { cp++; }; return cp - s; } and now I want to have: template <typename T> constexpr auto type_name_length = my_strlen(typeid(T).name()); But alas, typeid(T).name() is just const char* , not constexpr... is there some other, constexpr way to get a type's name? 回答1: Well, you could, sort of, but probably not quite portable:

Is std::vector<T> a `user-defined type`?

隐身守侯 提交于 2019-12-17 15:39:34
问题 In 17.6.4.2.1/1 and 17.6.4.2.1/2 of the current draft standard restrictions are placed on specializations injected by users into namespace std . The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the

Pass by value vs pass by rvalue reference

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 15:32:27
问题 When should I declare my function as: void foo(Widget w); as opposed to void foo(Widget&& w); ? Assume this is the only overload (as in, I pick one or the other, not both, and no other overloads). No templates involved. Assume that the function foo requires ownership of the Widget (e.g. const Widget& is not part of this discussion). I'm not interested in any answer outside the scope of these circumstances. See addendum at end of post for why these constraints are part of the question. The

Can I legally reinterpret_cast between layout-compatible standard-layout types?

孤街浪徒 提交于 2019-12-17 14:44:09
问题 I'm writing a class that, assuming the answer to Are enumeration types layout compatible with their underlying type? is "yes", is layout-compatible struct kevent but uses enum class es for filter , flags , etc. with the proper underlying types for the relevant fields. It is also standard-layout (the fields are all private and all themselves standard layout, there are no virtual members, there are no base classes). From my reading of n3690 , I can determine that my class and struct kevent have