Я пытаюсь понять, как std :: enable_if работает с параметрами шаблона inn.enable_if customization custom traits
#include <type_traits>
#include <iostream>
#include <memory>
using namespace std;
class Interface {};
class Value {};
class Stream
{
public:
template<typename T,
typename enable_if<
is_base_of< Interface, T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing interface class" << endl;
}
template<typename T,
typename enable_if<
is_base_of< Value, T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing value class" << endl;
}
};
class UserI : public Interface{};
class User : public Value{};
int main(int, char**)
{
auto interface = make_shared<UserI>();
auto value = make_shared<User>();
Stream s;
s.write(interface);
s.write(value);
return 0;
}
Их я пытался упростил код, обеспечивая собственные черты, чтобы проверить, если объект является экземпляром интерфейса или значения.
#include <type_traits>
#include <iostream>
#include <memory>
using namespace std;
class Interface {};
class Value {};
template<typename T>
struct is_interface
{
const static bool value = is_base_of< Interface, T>::value;
};
template<typename T>
struct is_value
{
const static bool value = is_base_of< Value, T>::value;
};
class Stream
{
public:
template<typename T,
typename enable_if<
is_interface<T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing interface class" << endl;
}
template<typename T,
typename enable_if<
is_value<T>{}
>::type* = nullptr>
void
write(const shared_ptr<T>& v)
{
cerr << "Writing value class" << endl;
}
};
class UserI : public Interface{};
class User : public Value{};
int main(int, char**)
{
auto interface = make_shared<UserI>();
auto value = make_shared<User>();
Stream s;
s.write(interface);
s.write(value);
return 0;
}
Но вторая версия не может скомпилировать со следующей ошибкой:
test_t2.cc: In function ‘int main(int, char**)’:
test_t2.cc:58:26: error: no matching function for call to ‘Stream::write(std::shared_ptr<UserI>&)’
s.write(interface);
^
test_t2.cc:58:26: note: candidates are:
test_t2.cc:32:9: note: template<class T, typename std::enable_if<is_interface<T>{}>::type* <anonymous> > void Stream::write(const std::shared_ptr<_Tp1>&)
write(const shared_ptr<T>& v)
^
test_t2.cc:32:9: note: template argument deduction/substitution failed:
test_t2.cc:30:28: error: could not convert template argument ‘is_interface<UserI>{}’ to ‘bool’
>::type* = nullptr>
Может кто-то объяснить, что проблема со второй версией?