Let's suppose that I've either a function or a class template that should work only for certains types, e.g. std::wstring and std::string.
I know that concepts can be used to put a constraint on a template so I would use something like this:
template <typename T>
concept StringLike = std::convertible_to<T, std::wstring> || std::convertible_to<T, std::string>;
template<StringLike S>
class A
{
public:
A(const S& data)
: data_{data}
{};
private:
S data_;
}
However, I was also thinking about template (class, in this case) instantiation which should achieve the same goal.
template<typename S>
class A
{
public:
A(const S& data)
: data_{data}
{};
private:
S data_;
}
template class A<std::string>;
template class A<std::wstring>;
If I understood their exchangeability correctly, when should I use one instead of the other?
PS. I know that std::convertible_to<T, std::string> is not exactly the same as template class A<std::string> since the first is less constrained than the latter but it's just for the sake of the example.