c++17

Local reference to std::cout captured by lambda without asking for it

前提是你 提交于 2019-12-01 02:04:28
Have I lost my mind? Was this always permitted? #include <iostream> int main() { auto& os = std::cout; auto write = []() { os << "what\n"; }; write(); } I'm using: Apple LLVM version 10.0.0 (clang-1000.10.44.4) Target: x86_64-apple-darwin17.7.0 Though also see on Coliru: ( live demo ) I always thought an empty capture would not capture anything. Indeed, MSDN says : An empty capture clause, [ ], indicates that the body of the lambda expression accesses no variables in the enclosing scope. Further research suggests that this is in fact okay for capturing const things (which I also didn't know,

Why is this nested lambda not considered constexpr?

一个人想着一个人 提交于 2019-12-01 01:49:00
问题 I'm trying to create a curried interface using nested constexpr lambdas, but the compiler does not consider it to be a constant expression. namespace hana = boost::hana; using namespace hana::literals; struct C1 {}; template < typename T, std::size_t size > struct Array {}; constexpr auto array_ = [] (auto size) { return [=] (auto type) { return hana::type_c<Array<typename decltype(type)::type, size()>>; }; }; int main() { constexpr auto c1 = hana::type_c<C1>; constexpr auto test = hana::type

static_cast'd pointer value

喜夏-厌秋 提交于 2019-12-01 00:54:51
问题 In the current draft standard (and C++17), this is written about static_casting a void * : A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T”, where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1 . If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the

GCC is no longer supported - C++17 - Android Studio

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-01 00:32:45
I need to use C++17 compliant source files in my Android project. I added my .cpp files to the src/main/cpp folder. After the build, this error appears: Build command failed. Error while executing process /Users/khasan/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {-H/Users/khasan/Projects/myapplication/app -B/Users/khasan/Projects/myapplication/app/.externalNativeBuild/cmake/debug/ arm64-v8a -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-21 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/khasan/Projects/myapplication/app/bu ild/intermediates/cmake/debug/obj/arm64-v8a -DCMAKE

Failure to deduce template argument std::function from lambda function

半世苍凉 提交于 2019-12-01 00:25:22
While exploring templates in C++, I stumbled upon the example in the following code: #include <iostream> #include <functional> template <typename T> void call(std::function<void(T)> f, T v) { f(v); } int main(int argc, char const *argv[]) { auto foo = [](int i) { std::cout << i << std::endl; }; call(foo, 1); return 0; } To compile this program, I am using the GNU C++ Compiler g++: $ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026 After compiling for C++11 , I get the following error: $ g++ -std=c++11 template_example_1.cpp -Wall template_example_1.cpp: In function ‘int main

Using std::launder to get a pointer to an active object member from a pointer to an inactive object?

送分小仙女□ 提交于 2019-11-30 23:25:24
问题 This question followes this one Let's consider this example code: struct sso { union{ struct { char* ptr; char size_r[8]; } large_str; char short_str[16]; }; bool is_short_str() const{ return *std::launder(short_str+15)=='\0'; //UB? } }; If short_str is not the active member dereferencing the pointer without std::launder would be UB. Let's consider that the ABI is well specified and that we know that size_r[7] is at the same address as short_str[15]. Does std::launder(short_str+15) return a

c++ generic compile-time for loop

做~自己de王妃 提交于 2019-11-30 23:17:50
问题 In some contexts, it could be useful/necessary to have a for loop evaluated/unrolled at compile time. For example, to iterate over the elements of a tuple , one needs to use std::get<I> , which depends on a template int parameter I , hence it has to be evaluated at compile time. Using compile recursion one can solve a specific problem, as for instance discussed here, here, and, specifically for std::tuple here. I am interested, however, on how to implement a generic compile-time for loop. The

Does boost::any / std::any store small objects in-place?

好久不见. 提交于 2019-11-30 22:30:49
问题 To hold arbitrarily large objects, boost::any / std::any surely needs to allocate heap space for objects. However, for small types whose size is less or equal to a pointer ( int,char,bool,... ), any could instead store the value in-place in the pointer slot or in some other in-place memory and not allocate heap space. But does the implementation do this? I have a scenario where I often store small types in an any and only sometimes larger types like string s. The code is quite hot and

C++1z Coroutines a language feature?

时光怂恿深爱的人放手 提交于 2019-11-30 20:27:21
Why will coroutines (as of now in the newest drafts for C++1z) be implemented as a core language feature (fancy keywords and all) as opposed to a library extension? There already exist a couple of implementations for them (Boost.Coroutine, etc), some of which can be made platform independent, from what i have read. Why has the committee decided to bake it into the core language itself? I'm not saying they shouldn't but Bjarne Stroustrup himself mentioned in some talk (don't know which one any more) that new features should be implemented in libraries as far as possible instead of touching the

Declaring a member function with a typedef coming from a metafunction

可紊 提交于 2019-11-30 20:07:05
Consider the following code: template <class> struct Foo_s { using type = void(); }; template <class> using Foo_u = void(); template <class T> struct Bar { Foo_u<void> foo1; // OK typename Foo_s<void>::type foo2; // OK Foo_u<T> foo3; // OK typename Foo_s<T>::type foo4; // Boom. }; template struct Bar<void>; The declaration of foo4 fails on GCC 7.2, Clang 5.0.0 and MSVC 19.10.25017. GCC: <source>: In instantiation of 'struct Bar<void>': 18 : <source>:18:17: required from here 15 : <source>:15:29: error: field 'Bar<void>::foo4' invalidly declared function type typename Foo_s<T>::type foo4; ^~~~