Question Details

No question body available.

Tags

c++ templates c++17 auto template-argument-deduction

Answers (1)

July 14, 2025 Score: 3 Rep: 71,217 Quality: Medium Completeness: 60%

Going on a hunch here because I'm no C++ expert. This is what I think is happening.

The compiler deduces construct because the base template declares Args... args. The specialization you defined for construct does not apply because it takes a const reference. The primary construct template does not have a definition available yet (only a forward declaration), therefore the return type cannot be deduced and the whole thing falls apart.

For construct, the deduction of std::optional is correct and matches the specialization. The return type can be deduced from there without looking at the base template.

Latest GCC also gives a somewhat clearer error consistent with the above:

: In function 'int main()':
:23:17: error: use of 'auto construct(Args ...) [with int Version = 1; Args = {std::optional}]' before deduction of 'auto'
   23 |     construct(o);    // fails
      |     ~~~~~~~~~~~~^~~
Compiler returned: 1

In order for construct to be correctly deduced you would have to declare the template as auto construct(const Args&... args) (or even Args&&). This would however break the deduction for construct flipping the problem. It seems to me like the only real solution would be providing the definition of construct to make both calls work as is in your code.