Question Details

No question body available.

Tags

c++ c++23

Answers (3)

Accepted Answer Available
Accepted Answer
December 17, 2025 Score: 16 Rep: 228,654 Quality: Expert Completeness: 100%

From cppinsight you might see the auto-generated deduction guide as well (comment mine):

template
Test(Arg&& arg1, Args&&... args) -> Test; // Empty Ts as Ts not deducible

And it is a better match than your explicit deduction guide for Test t{1, 2};

template  Test(Ts...) -> Test;

Several solutions:

  • Rewrite the explicit deduction guide to be a better match:
template  Test(T, Ts...) -> Test;

There is a tie-breaker explicit versus implicit.

  • Add constraints, so the constructor (implicit deduction guide) can be SFINAEd out. (requires (sizeof(T) != 0) on class/constructor, or std::is_constructible on constructor)
December 17, 2025 Score: 2 Rep: 749 Quality: Low Completeness: 80%

A possible solution is to constrain your constructor:

#include 
#include 

template class Test { public: template requires (requires { std::tuple{std::declval(), std::declval()...}; }) Test(Arg&& arg1, Args&&... args) : tuple{std::forward(arg1), std::forward(args)...} {}

private: std::tuple tuple; };

template Test(Ts...) -> Test;

int main() { Test t{1, 2}; }

This makes the implicitly generated deduction guide not usable because std::tuple cannot be initialized with two ints.

godbolt demo

Note that cppinsights does not show the constraints for the implicitly generated deduction guides.

December 17, 2025 Score: 1 Rep: 16,792 Quality: Low Completeness: 70%

Based on the cppinsights answer, a possible workaround would be to remove type deduction in the constructor itself, https://godbolt.org/z/Ksarv1Knd

#include 

template class Test { public: Test(T&&... args) : tuple{std::move(args)...} {} Test(T const&... args) : tuple{args...} {}

private: std::tuple tuple_; };

template Test(Ts...) -> Test;

int main() { Test t{1, 2}; }

Although I am now not entirelly sure if you need the const and non-const version, or if it makes any difference.

UPDATE: https://godbolt.org/z/36qzT6sfz