I've been looking for solution of my issue over Stack Overflow and saw many similar topics, but there were no answers pointing to so specific case.
In the attached code, everything works unless I uncomment #define ISSUE
. Then I'm getting many errors.
The goal here is to be able to only declare specializations in the class body. The implementation needs to be put in the same file after class definition.
Why this is not working? How to make it work?
My compiler supports C++17, nothing newer.
#include <cstdint>
#include <iostream>
#include <type_traits>
// #define ISSUE
template <typename T>
class Test
{
public:
template<typename U = T,
std::enable_if_t<
std::is_same<T, bool>::value ||
std::is_same<T, int8_t>::value ||
std::is_same<T, uint8_t>::value ||
(std::is_enum<T>::value && (sizeof(T) == 1U))
,int> = 0>
static void special(T data);
#if defined(ISSUE)
template<typename U = T,
std::enable_if_t<
std::is_same<T, int32_t>::value ||
std::is_same<T, uint32_t>::value ||
(std::is_enum<T>::value && (sizeof(T) == 4U))
,int> = 0>
static void special(T data);
#endif
};
template <typename T>
template <typename,
std::enable_if_t<
std::is_same<T, bool>::value ||
std::is_same<T, int8_t>::value ||
std::is_same<T, uint8_t>::value ||
(std::is_enum<T>::value && (sizeof(T) == 1U))
,int>>
void Test<T>::special(T data)
{
std::cout << "print 8-bit\n";
}
#if defined(ISSUE)
template <typename T>
template <typename,
std::enable_if_t<
std::is_same<T, int32_t>::value ||
std::is_same<T, uint32_t>::value ||
(std::is_enum<T>::value && (sizeof(T) == 4U))
,int>>
void Test<T>::special(T data)
{
std::cout << "print 32-bit\n";
}
#endif
int main()
{
Test<uint8_t>{}.special(5);
Test<int8_t>{}.special(5);
Test<bool>{}.special(true);
#if defined(ISSUE)
Test<uint32_t>{}.special(5);
Test<int32_t>{}.special(5);
#endif
}
Only the post:
helped a bit to get what works with #define ISSUE
commented out.