c++14

Partial specilization of static variable template in class template

我的梦境 提交于 2019-12-04 09:10:26
If I do partial specialization I got different results from clang and g++. template < typename T> class X { public: T i; X(T _i): i{_i}{} operator T(){ return i; } }; template < typename T2 > class Y { public: template <typename T> static X<T> x_in_y; }; template< typename T2> template< typename T> X<T> Y<T2>::x_in_y{200}; template<> template<> X<float> Y<int>::x_in_y<float>{100}; template<> template<> X<int> Y<int>::x_in_y<int>{101}; template< > template< typename T > X<T> Y<bool>::x_in_y{77}; int main() { std::cout << Y<int>::x_in_y<int> << std::endl; std::cout << Y<int>::x_in_y<float> <<

In C++14 can a constexpr member change a data member?

自闭症网瘾萝莉.ら 提交于 2019-12-04 09:06:57
In C++14, since constexpr are not implicitly const anymore, can a constexpr member function modify a data member of a class: struct myclass { int member; constexpr myclass(int input): member(input) {} constexpr void f() {member = 42;} // Is it allowed? }; Yes they are, I believe this change started with proposal N3598: constexpr member functions and implicit const and eventually became part of N3652: Relaxing constraints on constexpr functions which changed section 7.1.5 paragraph 3 what is allowed in the function body from a white-list: its function-body shall be = delete, = default, or a

Parameter packs not expanded with '…'

十年热恋 提交于 2019-12-04 08:31:07
问题 I have this code: #include <iostream> using namespace std; int print(int i) { cout << endl << i; } template<typename ...Args> inline void pass(Args&&...args) { } template<typename ...args> inline void expand(args&&... a) { print(a) ...; //this doesn't expand //pass( print(a)... ); this works } int main() { expand(1,2,3,4); return 0; } It throws an error: In function 'void expand(args&& ...)': error: expected ';' before '...' token print(a) ...; ^ parameter packs not expanded with '...': print

How do I use std::enable_if with a self-deducing return type?

≡放荡痞女 提交于 2019-12-04 08:28:30
问题 C++14 will have functions whose return type can be deduced based on the return value. auto function(){ return "hello world"; } Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom? For example, let's consider the following two functons: #include <type_traits> #include <iostream> //This function is chosen when an integral type is passed in template<class T > auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type { std::cout <<

Definition of standard-layout class in C++14

拥有回忆 提交于 2019-12-04 08:11:50
A standard-layout class is defined in [class]/7 in C++14, as follows (emphasis is mine): A standard-layout class is a class that: (7.1) — has no non-static data members of type non-standard-layout class (or array of such types) or reference, (7.2) — has no virtual functions (10.3) and no virtual base classes (10.1), (7.3) — has the same access control (Clause 11) for all non-static data members, (7.4) — has no non-standard-layout base classes, (7.5) — either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base classes

Using SFINAE with generic lambdas

谁说胖子不能爱 提交于 2019-12-04 07:56:08
Can generic lambdas take advantage of the "Substitution Failure Is Not An Error" rule ? Example auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< !is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; Are there any workarounds or plans to include this in the language ? Also since generic lambdas are templated function objects under the hood isn't it a bit odd that this can't be done ? Lambdas are function objects under

Filling a std::array at compile time and possible undefined behaviour with const_cast

笑着哭i 提交于 2019-12-04 07:47:54
It is known that std::array::operator[] since C++14 is constexpr , see declaration below: constexpr const_reference operator[]( size_type pos ) const; However, it is also const qualified. This causes implications if you want to use the subscript operator of a std::array in order to assign values to your array at compile time. For example consider the following user literal: template<typename T, int N> struct FooLiteral { std::array<T, N> arr; constexpr FooLiteral() : arr {} { for(int i(0); i < N; ++i) arr[i] = T{42 + i}; } }; The above code won't compile if you try to declare a constexpr

different compare signature for std::upper_bound and std::lower_bound

空扰寡人 提交于 2019-12-04 06:29:06
Here is a sample example for both std::lower_bound & std::upper_bound , notice the signature of Compare lambda being passed to them - const auto lower_x = std::lower_bound( points.begin(), points.end(), rec.min_corner.x, [](const RankedPoint &rp, const double x) { return rp.point.x < x; }); const auto upper_x = std::upper_bound( points.begin(), points.end(), rec.max_corner.x, [](const double x, const RankedPoint &rp) { return x < rp.point.x; }); What is the possible reasoning behind keeping the signature exact opposite of each other? I wasn't aware of this and gcc compiled (clang didn't) it

How does an unspecified pointer conversion behave in C++14?

故事扮演 提交于 2019-12-04 05:49:38
The result of some pointer casts are described as unspecified. For example, [expr.static.cast]/13: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” [...] If the original pointer value represents the address A of a byte in memory and A satisfies the alignment requirement of T, then the resulting pointer value represents the same address as the original pointer value, that is, A. The result of any other such pointer conversion is unspecified . My question is: in the case where alignment is not satisfied, what are the possible results? For example,

Member detection using void_t

…衆ロ難τιáo~ 提交于 2019-12-04 05:08:04
For member detection in C++14 I used code based on the example here , but it does no seem to work. A complete example: #include <string> template <typename...> using void_t = void; template <typename, typename = void> class HasMember_substr : public std::false_type {}; template <typename T> class HasMember_substr<T, void_t<typename T::substr>> : public std::true_type {}; template <typename, typename = void> class HasMember_fff : public std::false_type {}; template <typename T> class HasMember_fff<T, void_t<typename T::fff>> : public std::true_type {}; static_assert(HasMember_substr<std::string