c++17

Why is template parameter pack used in a function argument type as its template argument list not able to be explicit specified

允我心安 提交于 2019-12-03 08:08:53
问题 I have the following piece of code: template <typename, typename> struct AAA{}; template<typename ...Args> void f(AAA<Args...> *) {} int main() { f<int, int>(nullptr); } This code results in a compile error. When compiling using g++ -std=c++1z the error shows as follows: prog.cc: In function 'int main()': prog.cc:8:24: error: no matching function for call to 'f<int, int>(std::nullptr_t)' f<int, int>(nullptr); ^ prog.cc:5:6: note: candidate: template<class ... Args> void f(AAA<Args ...>*) void

Perfect forwarding with class template argument deduction

北城以北 提交于 2019-12-03 07:57:12
I would like to understand how deductions guides work with universal references and std::forward , in particular to create perfectly forwarding wrappers. The code below provides a code to experiment with a functor wrapper in two cases: one with an implicit deduction guide and one with an explicit deduction guide. I have put a lot of && and std::forward in comments, because I do not know where they are needed to achieve perfect forwarding. I would like to know where to put them, and where they are not needed. // Case with not conversion constructor template <class F> struct functor1 { explicit

With guaranteed copy elision, why does the class need to be fully defined?

天涯浪子 提交于 2019-12-03 06:49:50
问题 A followup to this post. Consider the following: class C; C foo(); That is a pair of valid declarations. C doesn't need to be fully defined when merely declaring a function. But if we were to add the following function: class C; C foo(); inline C bar() { return foo(); } Then suddenly C needs to be a fully defined type. But with guaranteed copy elision, none of its members are required. There's no copying or even a move, the value is initialized elsewhere, and destroyed only in the context of

std::any without RTTI, how does it work?

扶醉桌前 提交于 2019-12-03 06:46:11
问题 If I want to use std::any I can use it with RTTI switched off. The following example compiles and runs as expected also with -fno-rtti with gcc. int main() { std::any x; x=9.9; std::cout << std::any_cast<double>(x) << std::endl; } But how std::any stores the type information? As I see, if I call std::any_cast with the "wrong" type I got std::bad_any_cast exception as expected. How is that realized or is this maybe only a gcc feature? I found that boost::any did also not need RTTI, but I found

How to make a safer C++ variant visitor, similar to switch statements?

核能气质少年 提交于 2019-12-03 06:38:31
问题 The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: (snippet from cppreference.com) std::variant<int, long, double, std::string> v = ...; std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); The problem is when you put the wrong type in the visitor or change the variant signature

Nested template argument deduction for class templates not working

混江龙づ霸主 提交于 2019-12-03 06:29:41
In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates ( p0091r3 , p0512r0 ) #include <iostream> #include <iterator> #include <vector> template<class Rng> class Reverse { Rng const& rng; public: Reverse(Rng const& r) noexcept : rng(r) {} auto begin() const noexcept { using std::end; return std::make_reverse_iterator(end(rng)); } auto end() const noexcept { using std::begin; return std::make_reverse_iterator(begin(rng)); } }; int main() { std::vector<int> my_stack; my

How to get the file size in bytes with C++17

坚强是说给别人听的谎言 提交于 2019-12-03 06:28:11
问题 Are there pitfalls for specific operating systems, I should know of? There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today. Methods from other (old QA's) on .sx stat.h (wrapper sprintstatf), uses syscall tellg(), returns per definition a position but not necessarily bytes . The return type is not int . 回答1: <filesystem> (added in C++17) makes this very straightforward. #include

Why is `std::byte` an enum class instead of a class?

怎甘沉沦 提交于 2019-12-03 05:53:58
std::byte is an abstraction that is supposed to provide a type safe(r) access to regions of memory in C++, starting with the new standard 17. However, it's declared this way according to http://en.cppreference.com/w/cpp/types/byte : enum class byte : unsigned char {} ; That is, it is an enum class without any enumerations. Since usually the purpose of enums is to provide a restricted set of enumerations, this seems a bit strange. A class with a private unsigned char member seems like the more obvious way to do this. Why is it done this way? A class with an unsigned char member would not be

Is it safe to link gcc 6, gcc 7, and gcc 8 objects?

血红的双手。 提交于 2019-12-03 05:50:49
问题 Is it safe to link C++17, C++14, and C++11 objects asks about linking objects compiled with different language standards, and Jonathan Wakely's excellent answer on that question explains the ABI stability promises that gcc/libstdc++ make to enusure that this works. There's one more thing that can change between gcc versions though - the language ABI via -fabi-version. Let's say, for simplicity, I have three object files: foo.o , compiled with gcc 6.5 c++14 bar.o , compiled with gcc 7.4 c++14

Is there an existing name for this type and function?

走远了吗. 提交于 2019-12-03 05:40:54
问题 There are 2 hard problems in computer science: cache invalidation, naming things and off-by-one errors. This is about the 2nd problem: naming things. I'm looking if this technique or type has been used somewhere else already and has a name. dichotomy is an ok name, but bools_at_compile_time is a horrible one. using dichotomy_t = std::variant<std::false_type, std::true_type>; // (or a struct that inherits from that, and overloads operator bool()) constexpr dichotomy_t dichotomy( bool b ) { if