1
votes

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.

1
The explicit on the constructor probably makes a difference.Eljay
@Eljay but the constructor is not marked explicit in the headerRaees Rajwani
Ahh, yes, that was pre C++11. My mistake.Eljay
Which compiler are you using? I couldn't reproduce with any of recent gcc, clang and vc.Anton Savin
@AntonSavin I am using clang++ on MacOS.Raees Rajwani

1 Answers

1
votes

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