Pergunta

Estou tentando especializar uma metafunção em um tipo que tem um ponteiro de função como um de seus parâmetros. O código compila muito bem, mas simplesmente não corresponde ao tipo.

#include <iostream>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/identity.hpp>

template < typename CONT, typename NAME, typename TYPE, TYPE (CONT::*getter)() const, void (CONT::*setter)(TYPE const&) >
struct metafield_fun {};

struct test_field {};

struct test
{
  int testing() const { return 5; }
  void testing(int const&) {}
};

template < typename T >
struct field_writable : boost::mpl::identity<T> {};

template < typename CONT, typename NAME, typename TYPE, TYPE (CONT::*getter)() const >
struct field_writable< metafield_fun<CONT,NAME,TYPE,getter,0> > : boost::mpl::false_
{};

typedef metafield_fun<test, test_field, int, &test::testing, 0> unwritable;

int main()
{
  std::cout << typeid(field_writable<unwritable>::type).name() << std::endl;

  std::cin.get();
}

A saída é sempre o tipo que passou, nunca BOOL_.

Foi útil?

Solução

Como alternativa de trabalho sem os problemas de conversão mencionados nos comentários:

struct rw_tag {};
struct ro_tag {};

template<typename CONT, typename NAME, typename TYPE,
         TYPE (CONT::*getter)() const, void (CONT::*setter)(TYPE const&)>
struct metafield_fun_rw : rw_tag {};

template<typename CONT, typename NAME, typename TYPE,
         TYPE (CONT::*getter)() const>
struct metafield_fun_ro : ro_tag {};

template<class T> struct field_writable 
  : boost::mpl::bool_< boost::is_base_of<rw_tag, T>::value >
// or just derive directly from: boost::is_base_of<rw_tag, T>::value
{};

typedef metafield_fun_ro<test, test_field, int, &test::testing> unwritable;

alternativamente metafield_fun também poderia typedef readwrite-/readonly-tags, dependendo de seus argumentos e field_writable derivar condicionalmente, digamos usando boost::mpl::if_.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top