c++14

constexpr in C++11 and C++14 (not a difference to const keyword) [duplicate]

安稳与你 提交于 2020-01-02 07:09:56
问题 This question already has answers here : Difference between `constexpr` and `const` (8 answers) Closed 2 years ago . In the "Exploring C++17 and beyond" presentation by Mike Isaacson at one point (https://youtu.be/-ctgSbEfRxU?t=2907) there is question about writing: const constexpr .... vs single const. Mike said that in C++11 constexpr implies const and in C++14 it does not. Is it true? I've tried to find prove for that but I couldn't. I'm not asking about difference between const and

On the various ways of getting the result type of a function

大憨熊 提交于 2020-01-02 05:57:14
问题 In a context where the type of the result of a function call must be deduced, C++ seems to be more that happy to help us, providing (at least to my knowledge the following) two solutions : The result of type trait : std::result_of<F(Args...)>::type A core language syntax : decltype(std::declval<F>()(std::declval<Args>()...); My question is, are the any differences between the two ? Is there a context where one cannot be substituted by the other and if not why did we need a type trait to do

How to disengage std::experimental::optional?

╄→гoц情女王★ 提交于 2020-01-02 04:55:13
问题 With Boost I can create an optional in-place with: boost::optional<boost::asio::io_service::work> work = boost::in_place(boost::ref(io_service)); And disengage it with: work = boost::none; With C++14 / experimental support, I can instead construct an optional in-place with: std::experimental::optional<boost::asio::io_service::work> work; work.emplace(boost::asio::io_service::work(io_service)); But I'm at a loss for how to disengage it... 回答1: work = std::experimental::nullopt; should

Undefined reference error when initializing unique_ptr with a static const

江枫思渺然 提交于 2020-01-02 01:58:21
问题 When I try to use a static const to initialize a unique_ptr , I get an "undefined reference" error. However, when I new a pointer using the same constant, the symbol seems to be magically defined. Here is a simple program that reproduces the error: Outside_library.h class Outside_library { public: static const int my_const = 100; }; main.cpp #include "Outside_library.h" #include <iostream> #include <memory> class My_class { public: My_class(int num) { m_num = num; }; virtual ~My_class(){};

Is assert usable in constant expressions?

六眼飞鱼酱① 提交于 2020-01-02 00:57:10
问题 The assert -macro from <cassert> provides a concise way of ensuring that a condition is met. If the argument evaluates to true , it shall not have any further effects. However, can its invocation also be used inside a constant expression in that case? 回答1: This was dealt with by LWG 2234, which was brought back to attention after relaxed constraints on constexpr functions had been introduced. Proposed resolution : This wording is relative to N3936. Introduce the following new definition to

Is it possible to ignore the trailing return type feature of c++11 in favor of the function return type deduction feature of c++14?

戏子无情 提交于 2020-01-02 00:56:14
问题 When I skip the return type of an expression The following code in C++11 : auto function(X x, Y y) -> decltype(x + y) { return x + y; } Is equal to the following code in C++14 : decltype(auto) function(X x, Y y) { return x + y; } But additionally it is possible to deduce the return type without decltype rules in C++14 : auto function() { return 0; } When I know what the return type is exactly The following code in C++11 : auto function() -> int { return 0; } Is equal to the following code in

C++1y/C++14: Assignment to object outside its lifetime is not allowed in a constant expression?

你离开我真会死。 提交于 2020-01-02 00:34:30
问题 Is the following C++14/C++1y program ill-formed according to the current draft? #include <cstddef> template<typename T, size_t n> struct literal_array { T data[n]; }; template<typename T, size_t n, size_t m> constexpr literal_array<T, n+m> operator+(literal_array<T, n> a, literal_array<T, m> b) { literal_array<T, n+m> x; for (size_t i = 0; i < n; i++) x.data[i] = a.data[i]; for (size_t i = 0; i < m; i++) x.data[n+i] = b.data[i]; return x; } int main() { constexpr literal_array<int, 3> a = { 1

How to assign the address of an existing object to a smart pointer?

霸气de小男生 提交于 2020-01-01 23:58:07
问题 #include <memory> class bar{}; void foo(bar &object){ std::unique_ptr<bar> pointer = &object; } I want to assign an address of the object to the pointer. The above code obviously wont compile, because the right side of the assignment operator needs to be a std::unique_ptr. I've already tried this: pointer = std::make_unique<bar>(object) But it throws many errors during compilation. How can I do that? Update As said in the answers - using the std::unique_ptr::reset method led to undefined

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

☆樱花仙子☆ 提交于 2020-01-01 12:05:45
问题 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? }; 回答1: 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

using user-defined conversions with implicit conversions in comparisons

血红的双手。 提交于 2020-01-01 08:03:14
问题 I am struggling to understand why the following code does not allow an implicit conversion to occur. #include <string> using namespace std; struct HasConversionToString { HasConversionToString(const string& s_) : s{s_} {} string s; operator const string&() const { return s; } }; int main() { string s{"a"}; HasConversionToString obj{"b"}; return s < obj; } Both clang and gcc fail to find a valid way to compare the two objects with errors along the lines of: clang++ -std=c++14 -Wall -Wextra