The constructor takes T
by value, so string literals will decay to char const*
.
With the deduction guide Stack(char const*) -> Stack<std::string>
,
I expect both cases to work.
Why the second argument deduction Stack ss2 = "string literal"
does not?
#include<vector>
#include<string>
template<typename T>
class Stack {
std::vector<T> elems;
public:
Stack() = default;
Stack(T elm) : elems({std::move(elm)}) { }
};
Stack(char const*) -> Stack<std::string>;
void test() {
Stack ss1 { "works fine."};
// error: conversion from 'const char [15]' to
// non-scalar type 'Stack<std::__cxx11::basic_string<char> >'
// requested
Stack ss2 = "compile error!";
}
Compiler explorer : https://godbolt.org/g/z3PaBp