Why do constant expressions have an exclusion for undefined behavior?

The wording is actually the subject of defect report #1313 which says: The requirements for constant expressions do not currently, but should, exclude expressions that have undefined behavior, such as pointer arithmetic when the pointers do not point to elements of the same array. The resolution being the current wording we have now, so this … Read more

decltype and parentheses

Just above that example, it says if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e. if e is an lvalue, decltype(e) is T&, where T is the type of e; I think decltype(a->x) is an example of the “class member access” and … Read more

Are there any tricks to use std::cin to initialize a const variable?

I’d probably opt for returning an optional, since the streaming could fail. To test if it did (in case you want to assign another value), use get_value_or(default), as shown in the example. template<class T, class Stream> boost::optional<T> stream_get(Stream& s){ T x; if(s >> x) return std::move(x); // automatic move doesn’t happen since // return type … Read more

Move semantics and function order evaluation

Use list initialization to construct B. The elements are then guaranteed to be evaluated from left to right. C(std::unique_ptr<A> a) : B{a->x, std::move(a)} {} // ^ ^ – braces From ยง8.5.4/4 [dcl.init.list] Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which … Read more

Set std::vector to a range

You could use std::iota if you have C++11 support or are using the STL: std::vector<int> v(14); std::iota(v.begin(), v.end(), 3); or implement your own if not. If you can use boost, then a nice option is boost::irange: std::vector<int> v; boost::push_back(v, boost::irange(3, 17));