Raees Rajwani
Raees Rajwani

Reputation: 517

Class template argument deduction fails leads to substitution failure

I have a simple program that I am attempting to use to test C++17's class template argument deduction.

#include <iostream>
#include <list>

int main(int argc, const char * argv[]) {
    const char* a = "Hello";
    std::list x(1, a);
    return 0;
}

I would like to std::list to deduce the list having type const char*. However when attempting to run this code I obtain the error No viable constructor or deduction guide for deduction of template arguments of 'list'. Specifically the constructor that should be matched to this list(size_type __n, const value_type& __x); reports an error saying:

Candidate template ignored: substitution failure [with _Tp = const char *, _Alloc = std::__1::allocator<const char *>]: 'size_type' is a protected member of 'std::__1::__list_imp<const char *, std::__1::allocator<const char *> >'

I am curious why this does not work and yet a program like this is completely well formed with std::pair able to easily deduce the arguments:

#include <iostream>
#include <list>

int main(int argc, const char * argv[]) {
    const char* a = "Hello";
    std::pair x(1, a);
    return 0;
}

Thank you.

Upvotes: 1

Views: 431

Answers (1)

bolov
bolov

Reputation: 75668

clang 5 and 6 and gcc 7 and 8 compile your code without problem. So you are using either a compiler that didn't implement correctly the deduction guides or a library that doesn't have the appropriate deduction guides for std::list

Upvotes: 1

Related Questions