Reputation: 1240
I have a class, which wraps an enum and provides string conversion for it. Now I introduced the template parameter 'fastStringConvert' which controls how the conversion made using SFINAE (found here: how can I use std::enable_if in a conversion operator?). The code compiles under MSVC, but fails under GCC and Clang.
error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
What could be the problem and how should I change the code?
The relevant parts of the code below or here: http://rextester.com/SYC74124
#include <map>
#include <string>
#include <type_traits>
template <
class SubClass,
typename EnumType,
bool fastStringConvert = true
>
class SmartEnum
{
public:
template <
typename SFINAEPostponer = EnumType,
typename = typename std::enable_if<fastStringConvert, void>::type
>
explicit operator const std::string&() const
{
auto name = SubClass::names().find((int)value);
if (name != SubClass::names().end())
{
return name->second;
}
else
{
static const std::string na("n.a.");
return na;
}
}
template <
typename SFINAEPostponer = EnumType,
typename = typename std::enable_if<!fastStringConvert, void>::type
>
explicit operator const std::string() const
{
auto name = SubClass::names().find((int)value);
if (name != SubClass::names().end()) return name->second;
else return std::to_string((int)value);
}
protected:
typedef const std::map<int, std::string> Names;
EnumType value;
};
enum class Foo_type : int { a, b, c };
struct Foo : SmartEnum<Foo, Foo_type, true>
{
typedef SmartEnum<Foo, Foo_type, true> Base;
static const Base::Names &names()
{
static const Base::Names names = { { 0, "a" }, { 1, "b" }, { 2,"c" }};
return names;
}
};
Upvotes: 1
Views: 659
Reputation: 217448
You have to use template argument from the method, else you have a hard error, something like:
template <
typename SFINAEPostponer = EnumType,
bool Cond = !fastStringConvert,
typename = typename std::enable_if<Cond, void>::type
>
explicit operator const std::string() const
BTW, better to use enable_if
as type instead of default value (to allow disable part):
template <
typename SFINAEPostponer = EnumType,
bool Cond = !fastStringConvert,
typename std::enable_if<Cond, void>::type* = nullptr
>
explicit operator const std::string() const
Upvotes: 5