I have a templated class but only part of the template arguments can be deduced from the constructor.
Is there a way to provide the rest of the template arguments inside angle brackets when calling the constructor?
Assume we're using C 17.
template<typename T1, typename T2>
struct S
{
T2 t2;
S(const T2& _t2) : t2{_t2} {}
void operator()(const T1& t1)
{
std::cout << t1 << ", " << t2 << '\n';
}
};
int main()
{
S<int, double> s {3.14};
std::function<void(int)> func = s;
func(42);
// What I want:
//S<int> s2 {3.14}; <- T1 is provided in the angle brackets, T2 is deduced
//std::function<void(int)> func2 = s;
//func2(42);
}
As far as I know we need to either provide all the template arguments in angle brackets or none of them and use CTAD. The problem is that I don't want to write all the template arguments (in my actual use case there's like 5-6 of them and they are quite verbose) but I also don't want to pass all the arguments in the constructor because some of them are not used to construct the object. I just need their types for the operator() method.
I cannot make the operator() method templated because I want to bind it to a std::function object and I cannot deduce the template parameter types during the bind. So that's why I need all the types in the wrapping class.
This partial template deduction exists for functions.
For example:
template<typename T1, typename T2>
void foo(const T2& t2)
{
T1 t1{};
std::cout << t1 << ", " << t2 << '\n';
}
int main()
{
foo<int>(3.4); //T1 is explicitly int, T2 is deduced to be double
}
My current solution is to exploit this feature and construct the object through a function:
template<typename U1, typename U2>
S<U1, U2> construct_S(const U2& t2)
{
return S<U1, U2>{t2};
}
int main()
{
auto s2 = construct_S<int>(1.5);
std::function<void(int)> func2 = s2;
func2(23);
}
I find this solution clumsy because we're using an external function to construct the object.
I was wondering if there's a cleaner solution for doing this.
Maybe something with deduction guides? I'm not sure.
CodePudding user response:
As mentioned in a comment, you can use a nested class such that the two parameters can be provided seperately (one explicitly the other deduced):
template<typename T1>
struct S {
template <typename T2>
struct impl {
T2 t2;
impl(const T2& _t2) : t2{_t2} {}
};
template <typename T2>
impl(const T2&) -> impl<T2>;
};
int main() {
S<int>::impl<double> s {3.14};
S<int>::impl s2 {3.14}; // <- T1 is provided in the angle brackets, T2 is deduced
}
I found this How to provide deduction guide for nested template class?. Though, the above code compiles without issues with both gcc and clang: https://godbolt.org/z/MMaPYGbe1.
If refactoring the class template is not an option, the helper function is a common and clean solution. The standard library has many make_xxx
functions, some of them were only needed before CTAD was a thing.
CodePudding user response:
The simplest way is to provide factory function which will take over deduction of types:
template<typename T1, typename T2>
auto makeS(T2 x) -> S<T1, T2>
{
return S<T1, T2>{x};
}
https://godbolt.org/z/4cPTdv7e3
CodePudding user response:
template<typename T2>
struct S
{
T2 t2;
S(const T2& _t2) : t2{_t2} {}
template<typename T1>
void operator()(const T1& t1)
{
std::cout << t1 << ", " << t2 << '\n';
}
};
int main()
{
S s {3.14}; // T1 not provided
std::function<void(int)> func2 = s; // T1 deduced by std function
func2(42); // works
}
I just removed T1
and made it a template argument of operator()
and everything works.