c++14

Should non-capturing generic lambdas decay to function pointers?

▼魔方 西西 提交于 2019-12-05 05:37:53
Consider the following code: int main() { auto l = [](auto){}; void(*p)(int) = l; } It works just fine both with GCC and clang . Let's consider the following slightly modified version: int main() { auto l = [](auto...){}; void(*p)(int) = l; } In this case, clang still accepts it while GCC rejects it . Is there any reason for which this code should be rejected or is it a bug of the compiler? I'm going to open an issue, but I'd like to know if there exists any proposal that could have been implemented by one of them and not by the other one. This is a known GCC parsing bug ( 64095 , 68071 ): []

Who is responsible for the shared state of futures and promises

久未见 提交于 2019-12-05 05:16:14
Who owns the shared state in futures and promises? In particular who is responsible for the construction and deletion of the shared state in these classes? Or is the shared state supposed to be reference counted? I am not able to get an answer by reading the docs for these on cppreference. The way I was thinking about it the easiest thing to do would be to have the std::promise class be responsible for the creation of the shared state, but then hand it off to the std::future that is fetched from the std::promise for deletion when the future is destroyed. But then this approach can lead to

How can I design storage that conforms to the standard's implementation of std::any?

扶醉桌前 提交于 2019-12-05 04:46:46
The standard working draft (n4582, 20.6.3, p.552) states the following suggestion for implementations of std::any : Implementations should avoid the use of dynamically allocated memory for a small contained object. [ Example: where the object constructed is holding only an int. —end example ] Such small-object optimization shall only be applied to types T for which is_nothrow_move_constructible_v is true. As far as I know, std::any can be easily implemented through type erasure/virtual functions and dynamically allocated memory. How can std::any avoid dynamic allocation and still destroy such

Unpacking a typelist

夙愿已清 提交于 2019-12-05 04:45:42
Lets say I have a function that takes just a type template parameter, I cannot change it's definition/implementation. template < typename T > void do_it(); Now I have a typelist defined a usual way, can't change it either: template< typename ...Ts > struct typelist; I want to implement a function that takes a typelist, and runs do_it() on every type: template< typename List > void do_them(); The only solution I found up 'till now is: template< typename T > void do_them_impl() { do_it<T>(); } template< typename T, typename Ts...> void do_them_impl() { do_it<T>(); do_them_impl<Ts...>(); }

Why does std::cbegin return the same type as std::begin

你。 提交于 2019-12-05 04:33:59
cppreference shows this signature for std::cbegin : template< class C > constexpr auto cbegin( const C& c ) -> decltype(std::begin(c)); Shouldn't it return something like C::const_iterator instead? c is a const reference, so std::begin(c) it will return whatever the const overload of C::begin() returns. For standard library types, this is a const_iterator . For an array type, it is a pointer to const . Note that this relies on other, non-standard library user defined C , being implemented sanely with a const overload for C::begin() that returns an iterator that gives you const access to the

clang vs gcc - empty generic lambda variadic argument pack

限于喜欢 提交于 2019-12-05 04:09:50
I think I found another "clang vs gcc" inconsistency between lambdas and callable objects. decltype(l)::operator() should be equivalent to C::operator() , but if variadic pack is left empty in the generic lambda, gcc refuses to compile: 15 : error: no match for call to '(main()::) (int)' l(1); 15 : note: candidate: decltype (((main()::)0u).main()::(x, )) (*)(auto:1&&, auto:2&&, ...) 15 : note: candidate expects 3 arguments, 2 provided 14 : note: candidate: template main():: auto l = [](auto&& x, auto&&...) { return x; }; 14 : note: template argument deduction/substitution failed: 15 : note:

Undefined reference error when initializing unique_ptr with a static const

主宰稳场 提交于 2019-12-05 04:06:00
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(){}; private: int m_num; }; int main(int, char* []) { My_class* p_class = new My_class(Outside_library::my

Simple constexpr LookUpTable in C++14

北战南征 提交于 2019-12-05 03:03:43
I am trying to make a simple LookUpTable based on an array of integers, where the idea is to have it calculated at compile time . Trying to make it possible to use it for any other future tables of various integer types I might have, I need it as a template . So I have a LookUpTable.h #ifndef LOOKUPTABLE_H #define LOOKUPTABLE_H #include <stdexcept> // out_of_range template <typename T, std::size_t NUMBER_OF_ELEMENTS> class LookUpTableIndexed { private: //constexpr static std::size_t NUMBER_OF_ELEMENTS = N; // LookUpTable T m_lut[ NUMBER_OF_ELEMENTS ] {}; // ESSENTIAL T Default Constructor for

Passing lambda as template parameter to templated by function-pointer function

早过忘川 提交于 2019-12-05 02:42:27
Looks like I cannot pass a no-capture lambda as a template parameter to a templated by function-pointer function. Am I doing it the wrong way, or is it impossible? #include <iostream> // Function templated by function pointer template< void(*F)(int) > void fun( int i ) { F(i); } void f1( int i ) { std::cout << i << std::endl; } int main() { void(*f2)( int ) = []( int i ) { std::cout << i << std::endl; }; fun<f1>( 42 ); // THIS WORKS f2( 42 ); // THIS WORKS fun<f2>( 42 ); // THIS DOES NOT WORK (COMPILE-TIME ERROR) !!! return 0; } It's mostly a problem in the language's definition, the following

Access C++14 lambda captures like struct members

不羁的心 提交于 2019-12-05 01:48:37
AFAIK, C++11/14 does not allow in-place definition of a new return type while defining a lambda. However, it seems a C++14 lambda capture expression essentially creates an anonymous type with one or more "members" and an operator (). So, why is that the compiler does not allow access to the captured members from outside the lambda. My feeble mind cannot handle the complexities of C++ but does it sound like a reasonable language extension to you? Here is an example. vector<string> words = { "Stack", "Overflow" }; auto l = [w = words](){}; // almost like a C# anonymous type cout << l.w[0]; //