Suppose the following class:
class JsonDocument
{
public:
using RootType = size_t;
constexpr static RootType ObjectRoot = 1;
constexpr static RootType ArrayRoot = 2;
template<
RootType T = ObjectRoot,
std::enable_if<T == ObjectRoot || T == ArrayRoot>
>
JsonDocument();
JsonDocument(const std::string &json_str);
};
I want to specialize JsonDocument constructor for both ObjectRoot and ArrayRoot. The problem is, as there are no arguments, T couldn't be inferred. Explicitly specializing it doesn't work, neither this way:
class JsonDocument
{
// ...
// Doesn't work
template<> JsonDocument<ObjectRoot>();
template<> JsonDocument<ArrayRoot>();
}
Nor this:
template<> JsonDocument::JsonDocument<ObjectRoot>()
{
}
template<> JsonDocument::JsonDocument<ArrayRoot>()
{
}
So is it impossible at all? Is there any way to do it?
P.S.: This question is different; as deduction can be made.
Make the whole class a template. I think this makes sense because you are going to select which root to use at compile time anyway. (C++17 example)