c++17

Constructor with non-type template arguments

╄→尐↘猪︶ㄣ 提交于 2019-12-31 03:17:14
问题 In this question it's stated that it's impossible to just directly use template arguments for class constructor, because if you write something like struct S{ template<typename T> S() { ... } } Then you have no way of calling this constructor. However, there're some workarounds to make this work, for example, through template argument deduction. But all of these workarounds I know are for type arguments only. So, the question is Are there any workarounds to make this work for non-type

What's the Difference Between floor and duration_cast?

纵然是瞬间 提交于 2019-12-30 08:15:13
问题 So in c++11 the Chrono Library provides, duration_cast: Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished And c++17's floor: Returns the greatest duration t representable in ToDuration that is less or equal to d So for all x will the result of these 2 calls be equal: chrono::duration_cast<chrono::seconds>(x) chrono::floor<chrono::seconds>(x) 回答1: As far as I can tell, same as the difference between static_cast and std

How to force class template argument deduction when constructing a class in its own member functions?

无人久伴 提交于 2019-12-30 08:07:22
问题 Consider following code: struct A {}; template <typename T> struct B { B(T) {} auto foo() {return B(A{});} // error: no matching function for call to 'B<int>::B(A)' }; auto foo() {return B(A{});} // compiles int main() { foo(); B b(0); b.foo(); } Try it live I understand why B::foo() doesn't compile: Inside of struct B<T> , B (as an injected-class-name) means B<T> unless it's explicitly used as a template. Which in this case prevents class template argument deduction. Let's say I can't do

structured bindings and range-based-for; supress unused warning in gcc

眉间皱痕 提交于 2019-12-30 06:12:53
问题 I want to traverse a map using structure bindings, ignoring the key: for (auto& [unused, val] : my_map) do_something(val); I have tried different options with gcc-7.2.0: // The warning is issued for ([[maybe_unused]] auto& [unused, val] : my_map) do_something(val); // Syntax error for (auto& [[[maybe_unused]] unused, val] : my_map) do_something(val); // The same two combinations above with [[gnu::unused]]. It seems that the [[maybe_unused]] attribute is not implemented yet for structure

structured bindings and range-based-for; supress unused warning in gcc

回眸只為那壹抹淺笑 提交于 2019-12-30 06:12:32
问题 I want to traverse a map using structure bindings, ignoring the key: for (auto& [unused, val] : my_map) do_something(val); I have tried different options with gcc-7.2.0: // The warning is issued for ([[maybe_unused]] auto& [unused, val] : my_map) do_something(val); // Syntax error for (auto& [[[maybe_unused]] unused, val] : my_map) do_something(val); // The same two combinations above with [[gnu::unused]]. It seems that the [[maybe_unused]] attribute is not implemented yet for structure

Structured bindings width

▼魔方 西西 提交于 2019-12-29 06:59:06
问题 Is it possible to determine how many variable names should I to specify in square brackets using structured bindings syntax to match the number of data members of a plain right hand side struct ? I want to make a part of generic library, which uses structured bindings to decompose arbitrary classes into its constituents. At the moment there is no variadic version of structured bindings (and, I think, cannot be for current syntax proposed), but my first thought is to make a set of overloadings

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

我的未来我决定 提交于 2019-12-28 11:40:51
问题 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],

Convert std::variant to another std::variant with super-set of types

吃可爱长大的小学妹 提交于 2019-12-28 06:45:12
问题 I have a std::variant that I'd like to convert to another std::variant that has a super-set of its types. Is there a way of doing it than that allows me to simply assign one to the other? template <typename ToVariant, typename FromVariant> ToVariant ConvertVariant(const FromVariant& from) { ToVariant to = std::visit([](auto&& arg) -> ToVariant {return arg ; }, from); return to; } int main() { std::variant<int , double> a; a = 5; std::variant <std::string, double, int> b; b = ConvertVariant

Try to understand compiler error message: default member initializer required before the end of its enclosing class

末鹿安然 提交于 2019-12-28 04:23:39
问题 I try next code with three compilers (msvc2017, gcc8.2, clang7.0) and msvc2017 works all the way, but gcc and clang not. I want to understand what is wrong with my code, and why compiler can't compile it. #include <cassert> #include <iostream> #include <cstdlib> class Downloader { public: struct Hints { int32_t numOfMaxEasyHandles = 8; //Hints(){} // <= if I uncomment this all works gcc+clang+msvc //Hints() = default; // <= if I uncomment this neither clang no gcc works (msvc - works) };

function template specialization generating link error [duplicate]

不羁岁月 提交于 2019-12-25 01:38:36
问题 This question already has answers here : multiple definition of template specialization when using different objects (4 answers) Explicit specialization of function templates causes linker error (2 answers) Closed 6 months ago . I had previously asked this question that involved using auto with variadic templates that generates a tuple and the proper way to iterate over them. User metalfox had provided me with this solution. I tried their solution and this is what my full code looks like