c++20

Why has std::accumulate not been made constexpr in C++20?

懵懂的女人 提交于 2021-02-08 12:16:59
问题 In C++20, many (most?) C++-standard-library algorithms have been made constexpr . Yet - std::accumulate has not. It seems like it could have been: template<class InputIt, class T> constexpr T accumulate(InputIt first, InputIt last, T init) { for (; first != last; ++first) { init = std::move(init) + *first; } return init; } So - is there a reason it wasn't constexpr 'ed as well? Note: This question was motivated by my answer to this question on compile-time accumulation. 回答1: P1645R1 was

Visual Studio 2019 rejects `bool concept` while gcc 8 doesn't compile concepts without `bool`

删除回忆录丶 提交于 2021-02-08 09:58:06
问题 I thought c++ concepts is a better method to write c++ templated code with better error messages and faster compile times , so I upgraded Visual Studio to 2019 and still waiting for clang to support concepts however I tested some simple code with msvc from visual studio 2019 and g++ 8 from mingw-w64 and I'm having some trouble. This is the test: #include <iostream> using namespace std; // this compiles under g++ 8 but not visual studio 2019 template <class T> bool concept CharT = std::is_same

std::source_location as non type template parameter

痞子三分冷 提交于 2021-02-08 05:34:26
问题 In my infinite quest to push limits of what can be used as non type template parameter I was trying to see if I can use std::source_location as non type template parameter. That failed with a weird message, since I presume source_location is some magical struct... type 'std::experimental::source_location' of non-type template parameter is not a structural type It failed, so I tried to workaround that with using .file_name, but that also fails (godbolt). note: candidate template ignored:

std::source_location as non type template parameter

♀尐吖头ヾ 提交于 2021-02-08 05:34:12
问题 In my infinite quest to push limits of what can be used as non type template parameter I was trying to see if I can use std::source_location as non type template parameter. That failed with a weird message, since I presume source_location is some magical struct... type 'std::experimental::source_location' of non-type template parameter is not a structural type It failed, so I tried to workaround that with using .file_name, but that also fails (godbolt). note: candidate template ignored:

Idiomatic way to write concept that says that type is a std::vector

不羁的心 提交于 2021-02-07 20:39:14
问题 I have the following code that implements following type traits: that type is std::vector that type is std::vector of ints It works but it is quite verbose. Is there a shorter/nicer way to write this using concepts? I know I can steal concepts from range-v3 or some other similar library, but let's assume I want to implement it myself. #include <iostream> #include <string> #include <type_traits> #include <vector> template <class T> struct is_vector { static constexpr bool value = false; };

Could type traits be restricted to not accept other type traits as arguments?

泪湿孤枕 提交于 2021-02-07 07:17:22
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

Could type traits be restricted to not accept other type traits as arguments?

假装没事ソ 提交于 2021-02-07 07:14:52
问题 Question may be weird so here is a brief motivational example: #include <vector> #include <type_traits> template <typename T> // workaround for gcc 8.3 where volatile int is not trivially copyable using is_tc = std::is_trivially_copyable<std::remove_cv<T>>; // static assert passes compile, oops static_assert(is_tc<std::vector<int>>::value); As you can see mistake is that I have passed the type trait itself to another type trait instead of passing ::type or using std::remove_cv_t . Obvious

Overloading of hidden friends by differences only in (mutually exclusive) requires-clauses: legal or an ODR-violation?

有些话、适合烂在心里 提交于 2021-02-07 06:13:31
问题 Consider the following class template, which contains two (hidden) friend declarations of the same friend (same function type ; see below), which also defines the friend (and the friend is thus inline), but with the definition conditional on (mutually exclusive) requires-clauses : #include <iostream> struct Base {}; template<int N> struct S : public Base { friend int foo(Base&) requires (N == 1) { return 1; } friend int foo(Base&) requires (N == 2) { return 3; } }; [dcl.fct]/8 states that

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

Constrained member functions and explicit template instantiation

泪湿孤枕 提交于 2021-02-07 01:21:45
问题 G++ and Clang++ agree that the following snippet is not valid C++: template<int dim, int rank> struct Tensor {}; template<int dim> double InnerProduct(Tensor<dim, 1> const &, Tensor<dim, 1> const &) { return 0.0; } template<int dim> double DoubleInnerProduct(Tensor<dim, 2> const &, Tensor<dim, 2> const &) { return 0.0; } template<int dim, int rank> class Field { private: static double Dot(Tensor<dim, rank> const &u, Tensor<dim, rank> const &v) requires (rank == 1) { return InnerProduct(u, v);