c++17

std::bind with variadic template and auto return type

扶醉桌前 提交于 2021-02-08 18:35:47
问题 Following the code in this question, I have a std::bind with a variadic template function. If I try to provide a function template with auto return, gcc rejects the program: #include <functional> template <typename... Args auto inv_impl(Args... a) { return (a + ...); } template <typename... Args> auto inv(Args... args) { auto bound = std::bind(&inv_impl<Args...>, args...); return bound; } int main() { auto b = inv(1, 2); } The compile error is: foo.cc: In instantiation of ‘auto inv(Args ...)

g++ breaking change in std::filesystem::last_write_time

喜欢而已 提交于 2021-02-08 17:38:07
问题 With recent OS upgrade I noticed that small part of my code stopped compiling - it seems the reason is switching from g++-8 to g++-9 . This code is compiling alright on g++ 8.3.0 (verified this using gcc:8.3 image from Dockerhub) #include <filesystem> #include <chrono> int main() { namespace fs = std::filesystem; using namespace std::chrono_literals; std::filesystem::last_write_time("test", std::chrono::system_clock::now() - 5min); } On g++ 9.1.0 it fails with: test.cpp: In function 'int main

How does guaranteed copy elision work?

旧城冷巷雨未停 提交于 2021-02-08 12:41:48
问题 At the 2016 Oulu ISO C++ Standards meeting, a proposal called Guaranteed copy elision through simplified value categories was voted into C++17 by the standards committee. How exactly does guaranteed copy elision work? Does it cover some cases where copy elision was already permitted, or are code changes needed to guarantee copy elision? 回答1: Copy elision was permitted to happen under a number of circumstances. However, even if it was permitted, the code still had to be able to work as if the

C++17 operator“”s for string_view?

。_饼干妹妹 提交于 2021-02-08 12:16:40
问题 Will C++17 contain a literal suffix for const char* to std::string_view conversion? auto str = "asdf"s; Will the type of str in the above statement be std::string or std::string_view ? 回答1: If we're to believe STL's comment, then yes, we'll have string view literal suffixes based on, I believe, P0403R0. If I understand things correctly s will stay a std::string literal suffix, while std::string_view will use sv . cout << "Hello, string_view literals!"sv << endl; cout << "Hello, string

C++ best practice: Pass use-only (not stored) lambda argument to function by const-reference or by forwarding-reference (a.k.a. universal-reference)

我是研究僧i 提交于 2021-02-08 11:35:50
问题 What is the best (i.e. most performant, most versatile) way to pass a lambda function as a parameter to a function which only uses (but does not store or forward) the lambda function? Option 1: Is passing it by const-reference the way to go? template <typename F> void just_invoke_func(const F& func) { int i = 1; func(i); } Option 2: Or is it better to pass it as a forwarding reference (universal reference)? template <typename F> void just_invoke_func(F&& func) { int i = 1; func(i); } Does the

How to deduce the size of a compile time a const char string with C++ templates?

百般思念 提交于 2021-02-08 11:30:48
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

How to deduce the size of a compile time a const char string with C++ templates?

不打扰是莪最后的温柔 提交于 2021-02-08 11:30:25
问题 I am trying to use clang++ to expand the template code from this post. Here is what I come up with. constexpr unsigned int requires_inRange(unsigned int i, unsigned int len) { return i >= len ? throw i : i; } class StrWrap { unsigned size_; char * const begin_; public: template< unsigned N > constexpr StrWrap( const char(&arr)[N] ) : begin_(arr), size_(N - 1) { static_assert( N >= 1, "not a string literal"); } constexpr char operator[]( unsigned i ) { return requires_inRange(i, size_), begin_

Why doesn't std::is_invocable work with templated operator() which return type is auto-deduced (eg. generic lambdas)

女生的网名这么多〃 提交于 2021-02-08 09:47:36
问题 c++17 introduces template <class Fn, class...ArgTypes> struct is_invocable: Determines whether Fn can be invoked with the arguments ArgTypes... . Formally, determines whether INVOKE(declval<Fn>(), declval<ArgTypes>()...) is well formed when treated as an unevaluated operand, where INVOKE is the operation defined in Callable . However, this template does not work with templated operator() which (direct or indirect) return type is auto-deduced: #include <type_traits> #include <iostream> struct

c++ Use std::enable_if to conditionally add getters to a variadic variant template

可紊 提交于 2021-02-08 09:12:22
问题 I am trying to add specializations for the case where my variant has any of int , float , bool , and others as template arguments. My attempt so far is: #include <iostream> #include <variant> #include <string> #include <type_traits> template<typename... Types> struct variant : std::variant<Types...> { using std::variant<Types...>::variant; template<typename T> const T& get() const { return std::get<T>(*this); } #define VARGET(X) typename std::enable_if<(std::is_same<Types, X>::value || ... ),

Is it possible / desirable to create non-copyable shared pointer analogue (to enable weak_ptr tracking / borrow-type semantics)?

心不动则不痛 提交于 2021-02-08 09:07:20
问题 Problem: Unique_ptrs express ownership well, but cannot have their object lifetimes tracked by weak_ptrs. Shared_ptrs can be tracked by weak_ptrs but do not express ownership clearly. Proposed solution: Derive a new pointer type (I'm going to call it strong_ptr) that is simply a shared_ptr but with the copy constructor and assignment operator deleted, so that it is hard to clone them. We then create another new borrowed_ptr type (which is not easily storable) to handle the temporary lifetime