c++14

using user-defined conversions with implicit conversions in comparisons

白昼怎懂夜的黑 提交于 2020-01-01 08:01:07
问题 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

Getting around copy semantics in C++

为君一笑 提交于 2020-01-01 08:00:06
问题 Please consider this code: class A { }; int main() { std::vector<A> test; test.push_back(A()); } The constructor and destructor will be called twice, also memory will be allocated twice and the object will copied, now not only is that potentially bad for performance it could also lead to run time errors, especially if there's some cleanup going on in the destructor. The way i'd usually get around this is to just create a vector of pointers instead : std::vector<A*> test; test.push_back(new A(

C++ Compiler allows circular definition?

孤街醉人 提交于 2020-01-01 07:31:11
问题 I ran into the following oddity when making a mistake writing some code for trees. I've stripped down this example a lot so it is only a Linear Tree. Basically, in the main() function, I wanted to attach a Node to my tree, but instead of attaching it to "tree.root", I attached it to just "root". However, to my surprise, not only did it all compile just fine, but I was able to call methods on the nodes . It only errored when I tried to access the "value" member variable. I guess my main

What is this C++14 construct called which seems to chain lambdas?

Deadly 提交于 2020-01-01 04:43:04
问题 This is a follow-up question on this one: Lambda-Over-Lambda in C++14, where the answers explain the code. It is about a lambda that creates another lambda which when called, calls the passed lambda and passes the return value to the original lambda, thus returning a new instance of the second lambda. The example shows how this way lambdas can be chained. Copy from the original question: #include <cstdio> auto terminal = [](auto term) // <---------+ { // | return [=] (auto func) // | ??? { //

Explicit default constructor

拈花ヽ惹草 提交于 2020-01-01 03:59:06
问题 This code compiles fine with GCC 5.X, MSVC, but GCC 6.X gives error: "converting to 'a' from initializer list would use explicit constructor 'a::a()'" , clang "chosen constructor is explicit in copy-initialization" . Removing explicit or changing to a c{} fixes the problem, but I`m curious why it works this way. class a { public: explicit a () {} }; struct b { a c; }; int main() { b d{}; } 回答1: b is an aggregate. When you initialize it using an initializer list, the elements in the list will

Pass parameters to std::thread wrapper

家住魔仙堡 提交于 2020-01-01 03:29:08
问题 I want to implement a small thread wrapper that provides the information if a thread is still active, or if the thread has finished its work. For this I need to pass the function and its parameters to be executed by the thread class to another function. I have a simple implementation that should work but cannot get it to compile, and I can't figure out what to do to make it work. Here is my code: #include <unistd.h> #include <iomanip> #include <iostream> #include <thread> #include <utility>

Managing trivial types

て烟熏妆下的殇ゞ 提交于 2020-01-01 02:09:32
问题 I have found the intricacies of trivial types in C++ non-trivial to understand and hope someone can enlighten me on the following. Given type T , storage for T allocated using ::operator new(std::size_t) or ::operator new[](std::size_t) or std::aligned_storage , and a void * p pointing to a location in that storage suitably aligned for T so that it may be constructed at p : If std::is_trivially_default_constructible<T>::value holds, is the code invoking undefined behavior when code skips

Does a reference declaration introduce a new name for the referent?

淺唱寂寞╮ 提交于 2020-01-01 01:59:08
问题 In this question we've learnt that RVO cannot be applied to an expression like p.first . In comments it was also suggested that RVO is generally not applied to an expression like r after a declaration like auto& r = p.first . It is less clear whether the standard mandates this behaviour. in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception

Is there any real use case for function's reference qualifiers?

独自空忆成欢 提交于 2020-01-01 01:27:05
问题 Recently I learned about function's reference qualifiers , e.g. struct foo { void bar() {} void bar1() & {} void bar2() && {} }; Where I might need this feature, is there any real use case for this language feature ? 回答1: There are basically two uses: To provide an optimized overload, for example to move a member out of a temporary object instead of having to copy it. Prevent misuse of an API. For example, no one would expect int a = 1 += 2; to work and this would also cause a compile error.

Redefinitions of constexpr static data members are allowed now? (but not inline const)?

六月ゝ 毕业季﹏ 提交于 2019-12-31 20:18:21
问题 The following fails to compile under both gcc and clang in c++14, but succeeds with c++1z: struct Cls { static constexpr int N = 0; }; constexpr int Cls::N; constexpr int Cls::N; The C++14 error is predictable: redefinition of ‘constexpr const int Cls::N’ What changed to make this legal? I found: n4659 10.1.5 [dcl.constexpr] A function or static data member declared with the constexpr specifier is implicitly an inline function or variable So I thought it might have to do with inline variables