c++17

constexpr and std::cout working on function but not in lambda

有些话、适合烂在心里 提交于 2019-11-29 14:46:18
问题 Why constexpr does not work with std::cout , but works with printf ? #include <iostream> constexpr void f() { std::cout << ""; } //error constexpr void g() { printf(""); } //ok And why std::cout works with lambdas constexpr ? #include <iostream> int main () { auto h = []() constexpr { std::cout << ""; }; //ok } 回答1: Technically, it doesn't work with any of them. From [dcl.constexr]: For a constexpr function or constexpr constructor that is neither defaulted nor a template, if no argument

Check if a type is passed in variadic template parameter pack

≯℡__Kan透↙ 提交于 2019-11-29 14:11:22
问题 I've heard somewhere, that using new C++1z syntax, it is really easy to check if a type is passed in variadic template parameter pack - apparently you can do this with code that is near one-line long. Is this true? What are those relevant features? (I tried looking through fold expressions but I can't see how to use them in that problem...) Here's how I solved the problem in C++11 for reference: #include <type_traits> template<typename T, typename ...Ts> struct contains; template<typename T>

C++ parameter pack with single type enforced in arguments

我的未来我决定 提交于 2019-11-29 14:10:11
问题 I want to be able to do the following: #include <array> struct blah { }; template<typename... Args> constexpr auto foo(Args&&... args) { return std::array<blah, sizeof...(Args)>{{ args... }}; } auto res = foo({}, {}); The following answers aren't satisfying: they just want to check that the parameter pack is of a single type, but I want to convert the values right to it in the arguments (else it does not work). C++ parameter pack, constrained to have instances of a single type? Parameter with

How to include C++ 17 headers with g++ 6.2.0 with -std=c++17 (optional, any, string_view, variant)

隐身守侯 提交于 2019-11-29 13:55:07
std::optional is in C++ 17, where it was std::experimental::optional before. I tried compiling a file which included <optional> , with the command: g++ -std=c++17 <filename>.cpp (in the Bash terminal). I get the following error: <filename>.cpp:5:20 fatal error: optional: No such file or directory #include <optional> ^ compilation terminated But I can #include <experimental/optional> just fine. Am I missing some header files? How can I include the optional header? I also can't include <any> , <string_view> or <variant> , getting the same error. You can't. GCC 6.2's support for C++17 is

Why GCC rejects std::optional for references?

核能气质少年 提交于 2019-11-29 13:42:48
std::optional<int&> xx; just doesn't compile for the latest gcc-7.0.0 snapshot. Does the C++17 standard include std::optional for references? And why if it doesn't? (The implementation with pointers in a dedicated specialization whould cause no problems i guess.) Nicol Bolas Because optional , as standardized in C++17, does not permit reference types. This was excluded by design. There are two reasons for this. The first is that, structurally speaking, an optional<T&> is equivalent to a T* . They may have different interfaces, but they do the same thing. The second thing is that there was

Can a non-type template parameter be of type “void*”?

五迷三道 提交于 2019-11-29 13:37:28
Yakk - Adam Nevraumont said : Non-type template parameters of type void* are not allowed in at least some versions of the standard. Is this true? If it is true, in which versions of the standard are non-type template parameters of type void* not allowed? (Note: as noted in a comment to answer another comment , this is about non-type template parameters , not template type arguments , which can be any valid type-id per [temp.arg.type] , including void* . TL;DR Template parameters of type void* are valid since C++20. They are invalid prior to C++20. C++20 C++20 relaxed the restrictions on the

Does C++17 forbid copy elision in a case where C++14 allowed it?

China☆狼群 提交于 2019-11-29 13:34:45
Consider the following: struct X { X() {} X(X&&) { puts("move"); } }; X x = X(); In C++14, the move could be elided despite the fact that the move constructor has side effects thanks to [class.copy]/31, This elision of copy/move operations ... is permitted in the following circumstances ... when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type In C++17 this bullet was removed. Instead the move is guaranteed to be elided thanks to [dcl.init]/17.6.1: If the initializer expression is a prvalue and the

Can std::is_invocable be emulated within C++11?

怎甘沉沦 提交于 2019-11-29 13:34:08
I'd like to use std::is_invocable, however we are using c++11 standard, while is_invocable is available only from c++17. Is there any way to emulate the functionality using c++11? Thank you You can try this implementation:) Taken from boost C++ libraries. I've tested it with VS2017 with standard C++14. template <typename F, typename... Args> struct is_invocable : std::is_constructible< std::function<void(Args ...)>, std::reference_wrapper<typename std::remove_reference<F>::type> > { }; template <typename R, typename F, typename... Args> struct is_invocable_r : std::is_constructible< std:

get<string> for variants fail under clang++ but not g++

流过昼夜 提交于 2019-11-29 13:07:41
The following code: variant<string> x = "abc"; cout << get<string>(x) << "\n"; works fine under g++ (version 7.2). However, when compiled under clang++ (version 5.0) using libstdc++, I get the following error in the get method: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.2.0/../../../../include/c++/7.2.0/variant:238:46: fatal error: cannot cast 'std::variant<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >' to its private base class 'std::__detail::__variant::_Variant_storage<false, std:: __cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>

Multiple return values (structured bindings) with unmovable types and guaranteed RVO in C++17

耗尽温柔 提交于 2019-11-29 12:16:42
问题 With C++ 17, we will have the possibility to return unmovable (including uncopyable) types such as std::mutex , via what can be thought of as guaranteed return value optimization (RVO): Guaranteed copy elision through simplified value categories: struct nocopy { nocopy(nocopy&) = delete; nocopy() = default; }; auto getRVO(){ return nocopy(); } We will also have structured bindings, allowing: tuple<T1,T2,T3> f(); auto [x,y,z] = f(); or (here also using my understanding of the feature template