The following code fails to compile
template<typename T>
struct A {
A(T) {}
};
int main() {
int x = 3;
A(x); // compile failure here
}
and the reasons seems to be explained here and here: A(x); is interpreted as A x;, a conflicting declaration of x.
This also explains one of clangd messages:
Candidate function template not viable: requires 1 argument, but 0 were provided
prova.cpp:8:7: error: no viable constructor or deduction guide for deduction of template arguments of 'A'
On the other hand, clangd also say:
Cannot use parentheses when declaring variable with deduced class template specialization type [deduced_class_template_compound_type]
What is this error actually referring to? I can only interpret it one way: without the parenthesis, I can provide the needed argument, e.g. A x{3};, but with the parenthesis I cannot declare a variable and also pass parameters to its constructor, e.g. A(x)(3); doesn't work, nor does A(x){3};.
Why this question?
In Chapter 11 from Functional Programming in C++, when exploring/explaining DSL, the author uses a template helper function to construct an object of a template class. The code, on the other hand, is assumed to be compiled with at least -std=c++17 (indeed, it makes use of other C++17-specific features, like fold expressions), so in principle the class can deduce the template type if a constructor taking an argument of that template type is provided, which is the case for the example. However, removing the helper function, and renaming the class to the name of the (removed) helper function, makes the new code fail to compile at the call site, where an object is constructed with the syntax className(objectOfOtherClass).