-2

Compiler can not deduce _t2 and reports this error:

could not deduce template argument for '_t2'

Any suggestions, without making any changes in main()?

#include <tuple>

template <
    typename                        _t1,
    template <typename> typename    _t2>
void test(
    _t1                     p1,
    std::tuple<int, _t2>    p2)
{
}

int main(int argc, char *argv[])
{
    test(
        1,
        {
            2,
            'a'
        });

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
VeNToR
  • 33
  • 6
  • 2
    ```template typename _t2``` means ```_t2``` is a type that has a ```typename``` template parameter. It should just be ```typename _t2``` – TARN4T1ON Oct 05 '21 at 20:36
  • @TARN4T1ON, so I get "could not deduce template argument for '_t2'" error... – VeNToR Oct 05 '21 at 20:39
  • When you replace the whole template definition ```template typename _t2>``` with ```template ```? I was talking about the whole second template parameter. – TARN4T1ON Oct 05 '21 at 20:41
  • Since `main()` cannot be changed, this seems to be a duplicate of [Is it possible to infer template parameters of tuple from brace-type initialization?](https://stackoverflow.com/questions/16703838/is-it-possible-to-infer-template-parameters-of-tuple-from-brace-type-initializat) – Drew Dormann Oct 05 '21 at 20:43
  • @TARN4T1ON, yes... I replaced. – VeNToR Oct 05 '21 at 20:43
  • @DrewDormann similar but not same. – VeNToR Oct 05 '21 at 20:45
  • 1
    Oh, yeah. Different place. You have to explicitly call the constructor of ```std::tuple``` in the argument passed to ```test```. So either ```std::tuple(2, 'a')``` or ```std::tuple { 2, 'a' }```, instead of an initializer list. – TARN4T1ON Oct 05 '21 at 20:46
  • 2
    @VeNToR The target Drew suggested is exactly the problem you have (once you change the template template parameter to a template parameter as TARN4TION mentioned). Why don't you think it's the same question? – cigien Oct 05 '21 at 20:48
  • @cigien, I think that compiler can deduce without using std::make_tuple ? – VeNToR Oct 05 '21 at 20:50
  • 1
    No, it can't (well, it could just use a `tuple` constructor, but that's the same thing). Regardless, your question is still the same as the target, right? Take some to read through that post, and see why `{}` can't be used if you want to infer the types. – cigien Oct 05 '21 at 20:53
  • Curly braces define an initializer list, which would then have to be implicitly cast to a ```std::tuple```, for which the basic initializer list does not have the necessary conversion function (proper term is cast operator). See https://en.cppreference.com/w/cpp/language/cast_operator – TARN4T1ON Oct 05 '21 at 20:55
  • @cigien, I will take a look and re-think, thanks... – VeNToR Oct 05 '21 at 20:56

1 Answers1

2

The problem is here:

test(
    1,
    {
        2,
        'a'
    });

The compiler cannot deduce curly braces. You should change that line to:

test(
    1,
    std::make_tuple(
        2,
        'a'
    ));

And your function to:

template <
    typename                        _t1,
    typename    _t2>
void test(
    _t1                     p1,
    std::tuple<int, _t2>    p2)
{
}
Captain Hatteras
  • 490
  • 3
  • 11