Class Template Argument Deduction, introduced in C++17, allows deducing missing template arguments for class template instantiation based on the initializer.
The following program
template
struct S {};
S (s);
is compiled by GCC with only a warning about the redundant parentheses around the declarator. However, Clang gives a hard error for the declaration
error: cannot use parentheses…
For some reason there is still lack of expected CTAD for std::initializer_list in clang:
std::initializer_list l{1,2,3}; // error in clang
Adding a user-defined guide like the following can fix the issue:
namespace std {
template
…
The following code compiles with gcc but not with clang:
template
class number {
T num;
public:
number(T num = 0): num(num) {}
template
friend auto add(T1 a, T2 b);
};
template
C++ has this helpful feature that says that the template parameter is implicit in the code inside a template class A.
However, for construction, this seems to clash with CTAD.
How can I make CTAD take precedence?
For example, here, there is an error…
The C style array constructor for span is specified as follows
template constexpr span(
type_identity_t (&arr)[N]) noexcept;
Why is type_identity_t necessary? instead of just:
template constexpr span(
…
In Timur Doumler's talk at CppCon 2022, "C++ Lambda Idioms", at around 45:19 he is discussing the lambda overload trick and says something like
In C++17, we had to write a deduction guide to make this work -- which
is like two more lines of code --…
I am reading about deduction guides in C++17. So say we have the following example:
template struct Custom
{
};
template struct Person
{
Person(Custom const&);
Person(Custom&&);
};
template…
Consider the following code:
template
struct D : B { };
D d{[]{ }};
gcc 12.x accepts it and deduces d to be D* type of lambda */> as expected.
clang 14.x rejects it with the following error:
:4:3: error: no viable…
If I have a widely-used class template called Foo that I want to rename to Bar without having to update all of its users atomically, then up until C++17 I could simply use a type alias:
template
class Bar {
public:
// Create a Bar…
I am trying to deduce a bool template argument by the choice of the class constructor.
A simple example:
template
class Subrange {
public:
Subrange(A a) requires (not Condition); /* create Subrange */
…
Problem:
I want to have a deduction guide for a class that takes a variable number
of objects that are constructed by a variadic template.
E.g.
template
struct y {
using values_t = std::tuple;
values_t values;
…
Let's start with a simple add method for class number:
class number {
int num;
public:
number(int num = 0): num(num) {}
operator int() const { return num; }
};
number add(number t1, number t2) {
return t1 + t2;
}
int main() {
…
#include
template
using vec = std::vector;
int main()
{
std::vector a{2,3};
// vec b{2,3}; // not going to work
}
Are we still forced to use macros? There are so many disadvantages in using them...