c++17

Why is there no support for concatenating std::string and std::string_view?

空扰寡人 提交于 2019-11-28 06:44:20
Since C++1z, we have std::string_view , a light-weight view into a contiguous sequence of characters that avoids unnecessary copying of data. Instead of having a const std::string& parameter, it is now often recommended to use std::string_view . However, one quickly finds out that switching from const std::string& to std::string_view breaks code that uses string concatenation as there is no support for concatenating std::string and std::string_view : std::string{"abc"} + std::string_view{"def"}; // ill-formed (fails to compile) std::string_view{"abc"} + std::string{"def"}; // ill-formed (fails

Modern C++ best way to implement a function with a variable number of int parameters [duplicate]

纵然是瞬间 提交于 2019-11-28 06:34:15
问题 This question already has answers here : Variable number of arguments in C++? (16 answers) C++ Variadic Function Templates of Known Type (4 answers) Is it possible to use 'enable_if' and 'is_same' with variadic function templates? (3 answers) Closed 5 days ago . In modern C++(ie, version>=11), what is best way to implement a function with a variables number of int parameters? I only want int s, not a general type. Each of the parameters is of type int and no other type is allowed. void foo(/

Spirit X3: parser with internal state

无人久伴 提交于 2019-11-28 05:54:18
问题 I want to efficiently parse large CSV-like files, whose order of columns I get at runtime. With Spirit Qi, I would parse each field with a lazy auxiliary parser that would select at runtime which column-specific parser to apply to each column. But X3 doesn't seem to have lazy (despite that it's listed in documentation). After reading recommendations here on SO, I've decided to write a custom parser. It ended up being pretty nice, but now I've noticed I don't really need the pos variable be

Undefined reference error with new filesystem library and clang++7

我只是一个虾纸丫 提交于 2019-11-28 05:53:10
问题 I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe. I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code. #include <iostream> #include <string> #include <experimental

How to enable /std:c++17 in VS2017 with CMake

不问归期 提交于 2019-11-28 05:50:48
I'm trying to add the /std:c++17 compiler flag to VS2017 with CMake. I'm using the "modern" cross-platform way so far: set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # -std=c++11 instead of -std=gnu++11 set(MY_CXX_COMPILE_FEATURES cxx_generic_lambdas cxx_range_for cxx_strong_enums) add_library(mylib INTERFACE) target_compile_features(mylib INTERFACE ${MY_CXX_COMPILE_FEATURES}) This adds /std:c++14 in VS2017 (which might be the default anyway?). However I'm having trouble switching this to C++17 (i.e. having it add /std:c++17 ). If I just add it

Why is the construction of std::optional<int> more expensive than a std::pair<int, bool>?

佐手、 提交于 2019-11-28 05:36:55
Consider these two approaches that can represent an "optional int ": using std_optional_int = std::optional<int>; using my_optional_int = std::pair<int, bool>; Given these two functions... auto get_std_optional_int() -> std_optional_int { return {42}; } auto get_my_optional() -> my_optional_int { return {42, true}; } ...both g++ trunk and clang++ trunk (with -std=c++17 -Ofast -fno-exceptions -fno-rtti ) produce the following assembly: get_std_optional_int(): mov rax, rdi mov DWORD PTR [rdi], 42 mov BYTE PTR [rdi+4], 1 ret get_my_optional(): movabs rax, 4294967338 // == 0x 0000 0001 0000 002a

Compile-time or runtime detection within a constexpr function

主宰稳场 提交于 2019-11-28 05:27:05
I was excited when constexpr was introduced in C++11, but I unfortunately made optimistic assumptions about its usefulness. I assumed that we could use constexpr anywhere to catch literal compile-time constants or any constexpr result of a literal compile-time constant, including something like this: constexpr float MyMin(constexpr float a, constexpr float b) { return a<b?a:b; } Because qualifying a function's return type only as constexpr does not limit its usage to compile-time, and must also be callable at runtime, I figured that this would be a way to ensure that MyMin can only ever be

C++17 template deduction guide not used for empty parameter set?

有些话、适合烂在心里 提交于 2019-11-28 05:16:41
问题 Consider the following reduced example which can also be viewed at https://godbolt.org/g/Et56cm: #include <utility> template <class T> struct success { T value; constexpr success(T &&v) : value(std::move(v)) { } constexpr success(const T &v) : value(v) { } }; template <> struct success<void> { }; template <class T> success(T /*unused*/)->success<T>; success()->success<void>; int main(void) { auto a = success{5}; // works auto b = success{}; // works auto c = success{"hello"}; // works auto d

Parameter with non-deduced type after parameter pack

≯℡__Kan透↙ 提交于 2019-11-28 04:50:33
问题 There is different behaviour in clang++ and g++ for the next program: #include <type_traits> #include <utility> template< std::size_t index, typename type > struct ref { type & value; }; template< std::size_t index, typename type > type && get(ref< index, type > const & r) { return std::forward< type >(r.value); } template< typename F, typename ...types, std::size_t ...indices > decltype(auto) apply_inverse(F & f, types &... values, std::index_sequence< indices... >) { struct : ref< indices,

How to use new std::byte type in places where old-style unsigned char is needed?

馋奶兔 提交于 2019-11-28 04:42:41
std::byte is a new type in C++17 which is made as enum class byte : unsigned char . This makes impossible to use it without appropriate conversion. So, I have made an alias for the vector of such type to represent a byte array: using Bytes = std::vector<std::byte>; However, it is impossible to use it in old-style: the functions which accept it as a parameter fail because this type can not be easily converted to old std::vector<unsigned char> type, for example, a usage of zipper library: /resourcecache/pakfile.cpp: In member function 'utils::Bytes resourcecache::PakFile::readFile(const string&)