I want to declare the constructor outside of the class but there is something wrong with the template code.
template <size_t size = 1>
class my_class
{
public:
template <class int_t>
constexpr my_class(int_t);
};
template<class int_t, size_t size>
constexpr my_class<size>::my_class(int_t num)
{
}
but this works:
template <size_t size = 1>
class my_class
{
public:
template <class int_t>
constexpr my_class(int_t) {};
};
or:
template <size_t size = 1>
class my_class
{
public:
constexpr my_class();
};
template<size_t size>
constexpr my_class<size>::my_class()
{
}
I have no idea why this doesn’t work.
I asked ChatGPT but it told me that there is no error?
>Solution :
The my_class constructor function is a template inside a template. You can’t combine templates inside templates as a single template, especially not in the wrong order.
You need two template declarations:
template<size_t size>
template<typename int_t>
constexpr my_class<size>::my_class(int_t num)
{
}
Please note that the order of the templates matters.