c++17

C++ direct list initialization with deleted default constructor

倾然丶 夕夏残阳落幕 提交于 2021-02-07 09:43:40
问题 I have the following class definition. I included a private x to make sure it is not an aggregate. class A { private: int x; public: A() = delete; A(std::initializer_list<int>) { printf("init\n"); } }; Now if I initialize this object with A a = A{} , it will say A::A() is deleted. I guess it is trying to call A::A() but it is deleted. If I comment out that line, so A::A() is automatically generated. Then if I run this code, I could see that it is calling A::A(std::initializer_list<int>) ! And

C++ direct list initialization with deleted default constructor

风流意气都作罢 提交于 2021-02-07 09:42:32
问题 I have the following class definition. I included a private x to make sure it is not an aggregate. class A { private: int x; public: A() = delete; A(std::initializer_list<int>) { printf("init\n"); } }; Now if I initialize this object with A a = A{} , it will say A::A() is deleted. I guess it is trying to call A::A() but it is deleted. If I comment out that line, so A::A() is automatically generated. Then if I run this code, I could see that it is calling A::A(std::initializer_list<int>) ! And

return type deduction of recursive function

微笑、不失礼 提交于 2021-02-07 06:52:17
问题 Recently, I read Barry's answer to this question Recursive lambda functions in C++11: template <class F> struct y_combinator { F f; // the lambda will be stored here // a forwarding operator(): template <class... Args> decltype(auto) operator()(Args&&... args) const { // we pass ourselves to f, then the arguments. // [edit: Barry] pass in std::ref(*this) instead of *this return f(std::ref(*this), std::forward<Args>(args)...); } }; // deduction guide template <class F> y_combinator(F) -> y

Checking if variadic template parameters are unique using fold expressions

为君一笑 提交于 2021-02-07 05:49:05
问题 Given a variadic template parameter pack, I want to check if all types given to it are unique using an inline constexpr bool and fold expressions. I trie something like this: template<class... T> inline static constexpr bool is_unique = (... && (!is_one_of<T, ...>)); Where is_one_of is a similar bool that works correctly. But this line doesn't compile regardless of what I put into is_one_of. Can this even be done using fold expressions, or do I need to use a regular struct for this purpose?

Using fold expressions to print all variadic arguments with newlines inbetween

落花浮王杯 提交于 2021-02-07 05:25:07
问题 The classic example for C++17 fold expressions is printing all arguments: template<typename ... Args> void print(Args ... args) { (cout << ... << args); } Example: print("Hello", 12, 234.3, complex<float>{12.3f, 32.8f}); Output: Hello12234.3(12.3,32.8) I'd like to add newlines to my output. However, I can't find a good way to do that, the best I've found so far: template<typename ... Args> void print(Args ... args) { (cout << ... << ((std::ostringstream{} << args << "\n").str())); } This

Why does aggregate initialization not work anymore since C++20 if a constructor is explicitly defaulted or deleted?

一世执手 提交于 2021-02-07 04:43:06
问题 I'm migrating a C++ Visual Studio Project from VS2017 to VS2019. I'm getting an error now, that didn't occur before, that can be reproduced with these few lines of code: struct Foo { Foo() = default; int bar; }; auto test = Foo { 0 }; The error is (6): error C2440: 'initializing': cannot convert from 'initializer list' to 'Foo' (6): note: No constructor could take the source type, or constructor overload resolution was ambiguous The project is compiled with /std:c++latest flag. I reproduced

c++17 efficiently multiply parameter pack arguments with std::array elements

僤鯓⒐⒋嵵緔 提交于 2021-02-07 02:51:15
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

c++17 efficiently multiply parameter pack arguments with std::array elements

流过昼夜 提交于 2021-02-07 02:50:55
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

c++17 efficiently multiply parameter pack arguments with std::array elements

拥有回忆 提交于 2021-02-07 02:50:41
问题 I want to efficiently multiply the arguments from a parameter pack with the elements of a std::array: int index(auto... Is, std::array<int,sizeof...(Is)> strides) { // pseudo-code // int idx = 0; // for(int i = 0; i < sizeof...(Is); ++i) // idx += Is[i] * strides[i]; // return idx; } I can't quite wrap my brain around this one. I started down the road of an index sequence, but I could figure out how to incorporate the summation. I am using c++17, so fold expressions are fair game if they

Function template overload resolution, dependent and non-dependent parameters

爷,独闯天下 提交于 2021-02-07 02:21:39
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the