1

Why does this code not work:

template <typename valueType>
struct ts
{
    inline ts( valueType v )
    {}
};

template <typename charType>
inline void test( ts <charType> str )
{}

void testfun( void )
{
    test( 1 ); // ERROR: ts <typename valueType> not deducible from int
}

But this one does?

void testfun( void )
{
    ts a = 1; // OK: ts <typename valueType> is ts <int> because constructor
}

In both cases we are dealing with a ts-template but in the first case it is an embedded template instantiation as function parameter and in the second case the template is directly deduced. Please elaborate using arguments of the C++20 standard.

rplgn
  • 111
  • 7
  • 1
    "*Please elaborate using arguments of the C++20 standard*" This has nothing to do with CTAD. Your use case provides the template parameter, so CTAD goes unused. – Nicol Bolas Aug 26 '21 at 18:53
  • 1
    Template deduction only works on the exact type. It doesn't do conversions. That's pretty much all there is to it. – super Aug 26 '21 at 18:59
  • @NicolBolas Could you explain how I provide the template parameter in the not-compiling example if the template parameter is left undeduced by the call to test? Based on my understanding, to deduce charType one would have to know the type of ts because charType is not used directly as type of function parameters. – rplgn Aug 26 '21 at 19:05
  • @super Simple to understand yet hard to accept. – rplgn Aug 26 '21 at 19:07
  • 1
    You _could_ postpone the creation of `str`. [Example](https://godbolt.org/z/j4rcK8M6f) – Ted Lyngmo Aug 26 '21 at 19:49
  • 1
    @TedLyngmo Thank you for your constructive suggestion! I totally see how postponing str materialization does help. Unfortunately your example does not compile under latest MSVC. It has no reason to fail other than misfortunate implementation decisions by Microsoft, right? https://godbolt.org/z/PYa6sPEzz – rplgn Aug 26 '21 at 20:12
  • 2
    @rplgn: "*Based on my understanding, to deduce charType one would have to know the type of ts `` because charType is not used directly as type of function parameters.*" Yes. The thing you're talking about is *function* template argument deduction, when a function template argument is deduced from the arguments used to call the function. It has different rules from deduction used to deduce a *class*'s template arguments from a set of parameters to a constructor. – Nicol Bolas Aug 26 '21 at 20:19
  • @rplgn Change the compiler argument to `/std:c++17`, `/std:c++20` or `/std:c++latest` for MSVC and it'll work – Ted Lyngmo Aug 26 '21 at 20:20
  • @NicolBolas Thanks. The issue is clear to me now. – rplgn Aug 26 '21 at 20:21
  • @TedLyngmo I am trying! https://godbolt.org/z/e1EPzeh4Y Also doing that thing on my local installation of MSVC, same issue. – rplgn Aug 26 '21 at 20:22
  • 1
    @rplgn Copy one of mine. You are not using an argument that the compiler recognizes. – Ted Lyngmo Aug 26 '21 at 20:23
  • 1
    @TedLyngmo I guess the issue was me being hectic. I like you alot man, you actually helped me workaround this issue! :) – rplgn Aug 26 '21 at 20:34

0 Answers0