c++14

What is the usage of lambda trailing return type auto?

纵饮孤独 提交于 2019-12-20 18:43:44
问题 What is the usage of adding -> auto in []() -> auto { return 4; } ? For me - it is not different than []() { return 4; } 回答1: It is auto by default. The Standard, [expr.prim.lambda]/4, reads: If a lambda-expression does not include a lambda-declarator , it is as if the lambda-declarator were () . The lambda return type is auto , which is replaced by the trailing-return-type if provided and/or deduced from return statements as described in [dcl.spec.auto]. My addition. So, -> auto itself is

Fixed-width integer literals in C++?

假如想象 提交于 2019-12-20 18:36:22
问题 C++11 first introduced support for defining new literals into C++ by means of user-defined literals . Does C++11 or later also predefine suffixes for fixed-width integer literals for types in <cstdint> ? 回答1: No. As of C++14 the only literal suffixes defined by the standard are provided by <chrono> , <complex> and <string> headers in the standard library. The <chrono> header defines the h , min , s , ms , us , ns suffixes for time durations, <complex> defines the i , il and if suffixes for

Fixed-width integer literals in C++?

梦想的初衷 提交于 2019-12-20 18:36:03
问题 C++11 first introduced support for defining new literals into C++ by means of user-defined literals . Does C++11 or later also predefine suffixes for fixed-width integer literals for types in <cstdint> ? 回答1: No. As of C++14 the only literal suffixes defined by the standard are provided by <chrono> , <complex> and <string> headers in the standard library. The <chrono> header defines the h , min , s , ms , us , ns suffixes for time durations, <complex> defines the i , il and if suffixes for

Why does “return (str);” deduce a different type than “return str;” in C++?

筅森魡賤 提交于 2019-12-20 18:30:03
问题 Case 1: #include <iostream> decltype(auto) fun() { std::string str = "In fun"; return str; } int main() { std::cout << fun() << std::endl; } Here, program work fine in Gcc Compiler. decltype(auto) is deduced to be the type of str . Case 2: #include <iostream> decltype(auto) fun() { std::string str = "In fun"; return (str); // Why not working?? } int main() { std::cout << fun() << std::endl; } Here, generated following error and Segmentation fault : In function 'decltype(auto) fun()': prog.cc

Making `std::get` play nice with SFINAE

心不动则不痛 提交于 2019-12-20 17:41:28
问题 std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux

Making `std::get` play nice with SFINAE

≯℡__Kan透↙ 提交于 2019-12-20 17:41:28
问题 std::get does not seem to be SFINAE-friendly, as shown by the following test case: template <class T, class C> auto foo(C &c) -> decltype(std::get<T>(c)) { return std::get<T>(c); } template <class> void foo(...) { } int main() { std::tuple<int> tuple{42}; foo<int>(tuple); // Works fine foo<double>(tuple); // Crashes and burns } See it live on Coliru The goal is to divert the second call to foo towards the second overload. In practice, libstdc++ gives: /usr/local/bin/../lib/gcc/x86_64-pc-linux

Integral constant passed by value, treated as constexpr?

好久不见. 提交于 2019-12-20 17:38:51
问题 Although I've used code like this before, and it's clear that the compiler has enough information to work, I don't really understand why this compiles: template <class T, class I> auto foo(const T& t, I i) { return std::get<i>(t); } int main() { std::cerr << foo(std::make_tuple(3,4), std::integral_constant<std::size_t, 0>{}); return 0; } Live example: http://coliru.stacked-crooked.com/a/fc9cc6b954912bc5. Seems to work with both gcc and clang. The thing is that while integral_constant has a

Lambda function with number of arguments determined at compile-time

被刻印的时光 ゝ 提交于 2019-12-20 10:38:30
问题 I would like to declare a lambda function with exactly N parameters, where N is a template argument. Something like... template <int N> class A { std::function<void (double, ..., double)> func; // exactly n inputs }; I could not think of a way to do this with the metafunction paradigm. 回答1: You can write a template n_ary_function with a nested typedef type . This type can be used as follows: template <int N> class A { typename n_ary_function<N, double>::type func; }; The following code

Is the Committee Draft of Standard C++14 public?

空扰寡人 提交于 2019-12-20 09:53:30
问题 As of last Saturday... This afternoon in Bristol, UK, the ISO C++ standards committee adopted generic lambdas, dynamic arrays (an improved version of C99 VLAs), variable templates, reader/writer locks, make_unique, optional, standard library user-defined literals, and a number of other language and library improvements – and approved the result as the feature-complete Committee Draft (CD) of Standard C++14 to be distributed for its primary international review ballot. I'm interested reading

Efficient way of matching entities to systems in an entity component system

余生颓废 提交于 2019-12-20 09:18:22
问题 I'm working on a data-oriented entity component system where component types and system signatures are known at compile-time. An entity is an aggregate of components. Components can be added/removed from entities at run-time. A component is a small logic-less class. A signature is a compile-time list of component types. An entity is said to match a signature if it contains all component types required by the signature. A short code sample will show you how the user syntax looks and what the