c++17

Template Argument Deduction Broken in Clang 6 for Temporary Objects

浪子不回头ぞ 提交于 2019-11-28 13:32:26
Template argument deduction appears to be broken in Clang 6 for temporary objects. g++ 8.1.0 compiles and runs the example correctly. Clang 6.0.0 and 6.0.2 both error at the indicated line with this message: error: expected unqualified-id Print{1,"foo"s,2}; /********** Broken in Clang **********/ All Other lines work correctly. The behavior is the same in both cases whether -std=c++17 or -std=c++2a is used. The Clang c++ Status Page indicates that template argument deduction was implemented as of Clang 5 (P0091R3, P0512R0). Is this a bug? Are there workarounds (e.g. compiler flags, not code

Deduction guide and variadic templates

我与影子孤独终老i 提交于 2019-11-28 13:27:23
Consider the following code: #include <tuple> #include <iostream> template <class T> struct custom_wrapper { template <class Arg> custom_wrapper(Arg arg): data(arg) {} T data; }; template <class Arg> custom_wrapper(Arg arg) -> custom_wrapper<Arg>; template <class... T> struct custom_tuple { template <class... Args> custom_tuple(Args... args): data(args...) {} std::tuple<T...> data; }; template <class... Args> custom_tuple(Args... args) -> custom_tuple<Args...>; int main(int argc, char* argv[]) { custom_wrapper<int> w1(42); // OK custom_wrapper w2(42); // OK custom_tuple<int> t1(42); // OK

Does C++ have a free function `size(object)`?

北城以北 提交于 2019-11-28 13:22:42
It seems that the way that most people find the size of a string is they just use the my_string.size() and it works fine. Well, I recently did an assignment for class where I did... if (size(my_string) < 5) store[counter].setWeight(stoi(my_string)); Instead of.... if (my_string.size() < 5) store[counter].setWeight(stoi(my_string)); But to my suprise my instructor, who I believe is running an older compiler, wasn't able to run that line of code. On my compiler it works both ways and I'm not quite sure why. A complete program (it outputs 4 for both): #include <string> #include <iostream> using

Does std::string need to store its character in a contiguous piece of memory?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 13:16:12
I know that in C++98, neither std::basic_string<> nor std::vector<> were required to use contiguous storage. This was seen as an oversight for std::vector<> as soon as it was pointed out, and, if I remember correctly, got fixed with C++03. I seem to remember having read about discussions requiring std::basic_string<> to use contiguous storage back when C++11 was still called C++0x, but I haven't followed the discussion closely back then, and am still restricted to C++03 at work, so I am not sure what became of it. So is std::basic_string<> required to use contiguous storage? (If so, then which

Passing std::filesystem::path to a function segfaults

一曲冷凌霜 提交于 2019-11-28 12:48:00
When I attempt to use std::filesystem::path as a function argument, it segfaults on my machine. Here is a minimal example: #include <filesystem> void thing(const std::filesystem::path& p) { return; } int main() { thing("test"); return 0; } This snippet results in the following backtrace from gdb: #0 0x0000563a5a3814b3 in std::vector<std::filesystem::__cxx11::path::_Cmpt, std::allocator<std::filesystem::__cxx11::path::_Cmpt> >::~vector (this=0x23, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/stl_vector.h:567 #1 0x0000563a5a38132c in std::filesystem::__cxx11::path::~path (this=0x3, __in

optional constructor with initializer_list

假如想象 提交于 2019-11-28 12:00:40
What is the purpose of this special constructor taking initializer list. Can someone give an example of when this will be useful? template <class U, class... Args> constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args); How is the above different from this? template <class... Args> constexpr explicit optional(in_place_t, Args&&... args); Ref: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3793.html#optional.object.ctor P.S. Not sure whether to use c++14 or c++1z tag. I think there should be tag for c++ techinical specification The reason for the two separate

No <optional> in MS Visual Studio 2013 - what to do?

我的梦境 提交于 2019-11-28 11:10:45
I want to use std::experimental::optional , but MSVS 2013 tells me it can't find the header. Why isn't it there? Can I roll my own based on code elsewhere? The C++14 proposal maybe? std::experimental::optional originates from the Boost.Optional library, and this implementation works well in Visual C++ 12.0 (though it differs a little ). Reference single-header implementation, based on the N3793 proposal paper, can be found here . The latest list of supported C++11/14/1z core and library features that are shipped with Visual Studio can be found from the Visual C++ Team blog , from this post in

What is run first inside a cout statement? (C++17)

允我心安 提交于 2019-11-28 11:08:22
问题 Say for example I have a long statement like cout << findCurrent() << "," << findLowest() << "," << findHighest() << "," << findThird()<<"\n"; would findCurrent() be run before findLowest() like logic dictates? 回答1: Since C++17 the functions are guaranteed to be called left-to-right, i.e. findCurrent() is called first, then findLowest() and so on. C++17 Standard references: [expr.shift]/4 (referring to the expression E1 << E2 ): The expression E1 is sequenced before the expression E2 . [over

Is there a better alternative to std::remove_if to remove elements from a vector?

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:58:31
The task of removing elements with a certain property from a std::vector or other container lends itself to a functional style implementation: Why bother with loops, memory deallocation and moving data around correctly? However the standard way of doing this in C++ seems to be the following idiom: std::vector<int> ints; ... ints.erase( std::remove_if(ints.begin(), ints.end(), [](int x){return x < 0;}), ints.end()); This example removes all elements less than zero from an integer vector. I find it not only ugly but also easy to use incorrectly. It is clear that std::remove_if cannot change the

Using std::visit with variadic template struct

限于喜欢 提交于 2019-11-28 08:30:42
I was trying to understand the following example which I got from http://en.cppreference.com/w/cpp/utility/variant/visit #include <iomanip> #include <iostream> #include <string> #include <type_traits> #include <variant> #include <vector> using var_t = std::variant<int, long, double, std::string>; template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; // what is this declaration imply??? template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>; int main() { std::vector<var_t> vec = {10, 15l, 1.5, "hello"}; for (auto& v: vec) { std::visit(overloaded { [](auto arg) {