c++14

SFINAE and noexcept specifier

纵饮孤独 提交于 2019-12-09 16:14:11
问题 Does an expression in noexcept specifier's parentheses participate in SFINAE during overload resolution of function templates? I want to make an wrapper for aggregates and want the std::is_constructible predicate to work properly for it: template< typename type > struct embrace : type { template< typename ...arguments > embrace(arguments &&... _arguments) noexcept(noexcept(type{std::forward< arguments >(_arguments)...})) : type{std::forward< arguments >(_arguments)...} // braces { ; } }; int

Overloaded function template disambiguation with `std::enable_if` and non-deduced context

大兔子大兔子 提交于 2019-12-09 14:17:51
问题 Consider the following code: template <typename T> struct dependent_type { using type = T; }; template <typename T> auto foo(T) -> std::enable_if_t<std::is_same<T, int>{}> { std::cout << "a\n"; } template<typename T> void foo(typename dependent_type<T>::type) { std::cout << "b\n"; } The first overload of foo can deduce T from its invocation. The second overload of foo is a non-deduced context. int main() { foo<int>( 1 ); // prints "b" foo<double>( 1.0 ); // prints "b" foo( 1 ); // prints "a"

std::common_type trait for user defined types

旧城冷巷雨未停 提交于 2019-12-09 13:17:38
问题 Since C++11 the type trait std::common_type was introduced. std::common_type determines the common type between all of its template arguments. In C++14 its helper type std::common_type_t was also introduce in order to make code that uses std::common_type type trait shorter. std::common_type is particular useful in overloaded arithmetic operators, e.g., template<typename T1, typename T2> std::common_type_t<T1, T2> operator+(T1 const &t1, T2 const &t2) { return t1 + t2; } It works fine if its

I'm trying to print a Chinese character using the types wchar_t, char16_t and char32_t, to no avail.

情到浓时终转凉″ 提交于 2019-12-09 13:07:27
问题 I'm trying to print the Chinese character 中 using the types wchar_t , char16_t and char32_t , without success (live example) #include <iostream> int main() { char x[] = "中"; // Chinese character with unicode point U+4E2D char y[] = u8"中"; wchar_t z = L'中'; char16_t b = u'\u4e2d'; char32_t a = U'\U00004e2d'; std::cout << x << '\n'; // Ok std::cout << y << '\n'; // Ok std::wcout << z << '\n'; // ?? std::cout << a << '\n'; // prints the decimal number (20013) corresponding to the unicode point U

How to fill an array of unique_ptr?

江枫思渺然 提交于 2019-12-09 12:57:47
问题 Is it possible to use std:fill to fill an array of unique_ptr s? The intention is to have distinct pointers to distinct objects which are initialized with the same parameters. For example: std::unique_ptr<int> ar[3]; std::fill(ar.begin(), ar.end(), make_unique_for_each_element_somehow<int>(1)); 回答1: No, but this is what std::generate is for. Instead of being given a single value that's copied throughout the target range, std::generate is given a "generator" function that creates each value as

Create a std::function type with limited arguments

半腔热情 提交于 2019-12-09 12:31:28
问题 Given the type of a callable function C , I want to get at compile time a std::function ; the type of which: has the same return type of function C the argument types are the first N argument types of function C This means that, for a given type void(int, char, double) and a given N , the type of the function is: N = 1 => result type: std::function<void(int)> N = 2 => result type: std::function<void(int, char)> N = 3 => result type: std::function<void(int, char, double)> N > 3 => compile time

As far as I can tell the function below is not constexpr, but the code compiles in clang and g++. What am I missing?

狂风中的少年 提交于 2019-12-09 09:25:06
问题 I got this example from §5.19/2 in N4140: constexpr int incr(int &n) { return ++n; } As far as I can tell, this is not a constexpr function. But the snippet compiles in clang and g++. See live example. What am I missing here? 回答1: In C++14 the rules for constexpr function were relaxed and the paper N3597: Relaxing constraints on constexpr functions. The paper goes into the rationale and the effects and it includes the following ( emphasis mine ): As in C++11, the constexpr keyword is used to

Is it legal to check whether the address of a subobject lies within the bounds of a containing object

我的未来我决定 提交于 2019-12-09 08:41:06
问题 2 Questions: Is the following code well formed with defined behaviour? Is there any possible c++ implementation in which it could assert? Code (c++11 and higher): #include <cassert> #include <utility> #include <ciso646> template<class T> auto to_address(T* p) { return reinterpret_cast<unsigned char const*>(p); } /// Test whether part is a sub-object of object template<class Object, class Part> bool is_within_object(Object& object, Part& part) { auto first = to_address(std::addressof(object)),

Explicit defaulted default constructor and aggregates

∥☆過路亽.° 提交于 2019-12-09 08:33:13
问题 How to explain the difference, when I compile #if 0 and #if 1 versions of the following code: #include <cstdlib> struct A { explicit A() = default; // explicitly defaulted or deleted constructors are allowed for aggregates (since C++11) #if 1 private : #endif int i; }; int main() { A a = {}; return EXIT_SUCCESS; } for #if 0 all is fine, compilation successful. for #if 1 compilation failed with error message: error: chosen constructor is explicit in copy-initialization What is the difference

`weak_ptr::expired` behavior in the dtor of the object

爱⌒轻易说出口 提交于 2019-12-09 08:05:31
问题 Consider the following code: #include <iostream> #include <memory> using namespace std; class T; std::weak_ptr<T> wptr; class T { public: T() { } ~T() { std::cout << "in dtor" << std::endl; std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl; } }; int main() { { auto ptr = std::make_shared<T>(); wptr = ptr; std::cout << (wptr.expired() ? "expired" : "not expired") << std::endl; } return 0; } In this code, I was trying to find out if weak_ptr s are expired in the objects