c++17

Clang and the binary fold expressions — The curse of the empty parameter pack

旧巷老猫 提交于 2019-12-05 14:29:16
问题 Specifically Clang 3.6.0, the one currently hosted by Coliru. All these snippets are called from : int main() { foo(); std::cout << "\n----\n"; foo(1, 2, 3); } The following code : template <class... Args> void foo(Args... args) { std::cout << ... << args; } Triggers the following compilation error : main.cpp:7:17: error: expected ';' after expression std::cout << ... << args; ^ ; main.cpp:7:15: error: expected expression std::cout << ... << args; ^ So I tried putting parentheses around the

How to use experimental parallel STL in C++1z?

痴心易碎 提交于 2019-12-05 13:45:25
I want to try parallel STL of C++17. However, I can't find experimental/execution_policy in libc++. How can I try this? I'm trying http://en.cppreference.com/w/cpp/experimental/reduce , which says I should include these files, but I cannot find execution_policy. #include <experimental/execution_policy> After I install libc++ (I followed http://libcxx.llvm.org/docs/BuildingLibcxx.html ), I tried the following commands, but in vain. $ clang++-3.5 -std=c++1z test.cpp -lc++experimental test.cpp:5:10: fatal error: 'experimental/execution_policy' file not found #include <experimental/execution

Why does including <utility> break structured bindings in GCC?

空扰寡人 提交于 2019-12-05 12:27:10
问题 Consider: struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } This code compiles fine with gcc 7.1 in C++17 mode, however this one: #include <utility> struct Point { int x, y; }; int main() { const auto [x, y] = Point{}; } gives an error: bug.cpp: In function 'int main()': bug.cpp:7:16: error: 'std::tuple_size<const Point>::value' is not an integral constant expression const auto [x, y] = Point{}; ^~~~~~ What's going on here? A compiler bug, or is this how structured

Template non-type parameter deduction

怎甘沉沦 提交于 2019-12-05 11:42:32
Is it possible to deduce template value (not type) for a c++17 function? The function foo: template<int I> int foo() { return (I); } Can be called via: foo<5>(); And will return 5. Template types can be deduced through the type of a function argument. Is it possible to do the same in some way for the template value? For example: template<int I = x> int bar(const int x) { return (I); } This obviously wont work (because for one x is required before its declaration), but might there be some C++17 trick which would allow for this? I'd like to use this as a way of setting a constant expression

Why does std::apply fail with a generic function?

别等时光非礼了梦想. 提交于 2019-12-05 11:08:24
Taken from cppreference , why does the call to std::apply(add_generic, ...) fail to compile? Is there a way to fix it? #include <iostream> #include <tuple> int add(int first, int second) { return first + second; } template<typename T> T add_generic(T first, T second) { return first + second; } int main() { std::cout << std::apply(add, std::make_tuple(1,2)) << '\n'; // template argument deduction/substitution fails std::cout << std::apply(add_generic, std::make_tuple(2.0f,3.0f)) << '\n'; } It fails with error: [x86-64 gcc 7 (snapshot)] error: no matching function for call to 'apply(, std::tuple

Implementing rvalue references as parameters in function overloads

非 Y 不嫁゛ 提交于 2019-12-05 10:10:21
I've already asked on code review and software engineering but the topic didn't fit the site, so I'm asking here hoping this is not opinion-based. I am an "old school" C++ developer (I've stopped at C++ 2003) but now I've read a few books on modern C++ 11/17 and I'm rewriting some libraries of mine. The first thing I've made is adding move constructor/assignment operator where needed ( = classes that already had destructor + copy constructor and copy assignment). Basically I'm using the rule of five . Most of my functions are declared like func(const std::string& s); Which is the common way to

if constexpr instead of tag dispatch

守給你的承諾、 提交于 2019-12-05 09:58:51
I want to use if constexpr instead of tag dispatching, but I am not sure how to use it. Example code below. template<typename T> struct MyTag { static const int Supported = 0; }; template<> struct MyTag<std::uint64_t> { static const int Supported = 1; }; template<> struct MyTag<std::uint32_t> { static const int Supported = 1; }; class MyTest { public: template<typename T> void do_something(T value) { // instead of doing this bool supported = MyTag<T>::Supported; // I want to do something like this if constexpr (T == std::uint64_t) supported = true; } }; One way is to define a constexpr

std::any across shared library bounding in mingw

别等时光非礼了梦想. 提交于 2019-12-05 09:57:33
I stumbled about an issue while using libstdc++'s std::any implementation with mingw across a shared library boundary. It produces a std::bad_any_cast where it obviously should not (i believe). I use mingw-w64, gcc-7 and compile the code with -std=c++1z. The simplified code: main.cpp: #include <any> #include <string> // prototype from lib.cpp void do_stuff_with_any(const std::any& obj); int main() { do_stuff_with_any(std::string{"Hello World"}); } lib.cpp: Will be compiled into a shared library and linked with the executable from main.cpp. #include <any> #include <iostream> void do_stuff_with

Why allow shared_ptr<T[N]>?

喜你入骨 提交于 2019-12-05 09:46:43
问题 This answer cites N4082, which shows that the upcoming changes to std::shared_ptr will allow both T[] and T[N] variants: Unlike the unique_ptr partial specialization for arrays, both shared_ptr<T[]> and shared_ptr<T[N]> will be valid and both will result in delete[] being called on the managed array of objects. template<class Y> explicit shared_ptr(Y* p); Requires : Y shall be a complete type. The expression delete[] p , when T is an array type, or delete p , when T is not an array type,

`std::any_cast` returns a copy

早过忘川 提交于 2019-12-05 09:36:14
I was reading the documentation for std::any_cast and I find it strange that the API has the cast either return a value to the held object or a pointer to it. Why not return a reference? A copy needs to be made every time the function is called with a non pointer type argument. I can see that the pointer version of the cast might signal intentions a bit more and might be a bit more clear but why not have the value returned be a reference like this? template<typename ValueType> ValueType& any_cast(any* operand); instead of template <typename ValueType> ValueType* any_cast(any* operand); Further