c++14

C++14 lambda's default argument type deduction depending on preceding arguments

两盒软妹~` 提交于 2019-12-23 19:05:24
问题 Is this not valid as C++14? auto f = [](auto x, auto y = std::decay_t<decltype(x)>{}) { }; f(0); I was expecting it to be roughly equivalent to auto f = [](int x, int y) { }; f(0, int{}); Neither GCC 6.3 nor Clang 4.0 accepted my code. http://ideone.com/b7b4SK GCC http://ideone.com/EyLYaL Clang Is it related to my lack of understanding of C++ template deduction phases? Does the 1400 pages long spec actually has an explicit answer to my question? Update To summarize, my problem can in fact be

c++ parallel std::sort for floating values

≡放荡痞女 提交于 2019-12-23 17:14:29
问题 I've a large file consisting of > millions of floating point values. I can easily sort them using std::sort by reading file into vector for now, eg - std::vector<float> v; std::sort(v.begin(), v.end()); but is there any version of std::sort or similar algorithm which takes advantage of multiple cores available on my system? Since this is the only task that takes much time setting up, I'm looking for perf improvements from having > 1 core cpu. I can use any latest releases of compilers on a

How can a template parameter pack have other trailing arguments?

女生的网名这么多〃 提交于 2019-12-23 16:45:50
问题 In the C++14 draft standard, [temp.param]/11 says: If a template-parameter of a primary class template or alias template is a template parameter pack, it shall be the last template-parameter. If you try compiling the following template, then the compiler will complain. template< typename ...Args, void(*f)(Args...) > // ERROR struct Bar {}; But how does it work in this case? template< typename F, F > struct Bar; template< typename ...Args, void(*f)(Args...) > // OK ??? struct Bar< void(*)(Args

Using a this pointer in a generic lambda capture

左心房为你撑大大i 提交于 2019-12-23 16:07:42
问题 I have an issue in which Clang (3.6) and G++ (5.1) have a differing opinion: #include <functional> struct X { X() { std::function<void (int)> f = [this](auto x){foo(x);}; } void foo(int x){} }; int main(){} Clang accepts this, whereas G++ states: error: cannot call member function ‘void X::foo(int)’ without object Both compilers accept it if I call this->foo(x) directly instead, but I'd rather know who's right. Note: both the "auto" in the lambda signature and the conversion to a std:

How do we compose functions that return multiple values in C++ [duplicate]

∥☆過路亽.° 提交于 2019-12-23 15:54:57
问题 This question already has answers here : “unpacking” a tuple to call a matching function pointer (8 answers) Closed 4 years ago . How do we compose functions that return multiple return values in C++? More specifically, if one function returns a tuple, can we compose this function with another that does not explicitly accept tuples? For example, in the code: #include <tuple> #include <iostream> std::tuple <int,int> tuple_ints(int x,int y) { return std::tuple <int,int> (x,y); } int add(int x

Why is forwarding reference constructor called instead of copy constructor?

余生颓废 提交于 2019-12-23 15:47:57
问题 Given the following code #include <iostream> using namespace std; template <typename Type> struct Something { Something() { cout << "Something()" << endl; } template <typename SomethingType> Something(SomethingType&&) { cout << "Something(SomethingType&&)" << endl; } }; int main() { Something<int> something_else{Something<int>{}}; auto something = Something<int>{}; Something<int>{something}; return 0; } I get the following output Something() Something() Something(SomethingType&&) Why is the

Comma operator in C++11 (sequencing)

China☆狼群 提交于 2019-12-23 15:43:07
问题 The standard mentions f(a,(t=3,t+2),c); which would be an assignment-expression followed by an expression for the 2nd operator according to my understanding. But the grammar lists it juxtaposed: expression: assignment-expression expression, assignment-expression Working Draft, Standard for Programming Language C ++ Revision N4140 (November 2014) Is someone so nice as to explain to me please what it is that I'm missing here? 回答1: When you see expression: assignment-expression expression,

Composable C++ Function Decorators

南笙酒味 提交于 2019-12-23 14:56:56
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

Composable C++ Function Decorators

自作多情 提交于 2019-12-23 14:56:46
问题 Python has a very useful feature of function decorators, which, moreover, allows composition. For example, if write a function foo , then you can state that you would like foo to be memoized, but also retried more than a single time in case of a cache miss in which foo also raises an exception, by: @lru_cache @retry def foo(...): Decorator composability allows developing functions like foo and individual function decorators independently, and then mixing them as needed. It would be nice if we

std::cout usage in constructors of objects with static storage duration

我的未来我决定 提交于 2019-12-23 12:55:59
问题 Is it safe to use std::cout in constructors of objects with statc storage duration in C++98 / C++03? It seems from this answer that it isn't but it doesn't include any quotes from the standard. Is it only safe to do that in C++11 and C++14? 回答1: From C++14 (N3797), §27.4p2: The objects are constructed and the associations are established at some time prior to or during the first time an object of class ios_base::Init is constructed, and in any case before the body of main begins exe- cution