Что вызывает C4250 (класс наследует член через доминирование) при использовании расширенной сериализации с виртуальным базовым классом? - PullRequest
1 голос
/ 03 ноября 2010

Значение , означающее предупреждения компилятора VC ++ C4250 'class1' : inherits 'class2::member' via dominance, мне ясно.(Но см. здесь для объяснения.)

В настоящее время у меня проблема, что я получаю это предупреждение при сериализации иерархии классов , которая имеет абстрактный базовый класс с надстройкой:: serialization (1.44.0).

Обратите внимание, что мои классы не образуют какую-либо алмазоподобную иерархию наследования, которая могла бы вызвать это предупреждение, нопредупреждение вызывается созданием boost::detail::is_virtual_base_of_impl<...> при сериализации экземпляров производных классов.(Кажется, что используется is_virtual_base_of от Boost.TypeTraits.)


Вот минимальный пример кода для воспроизведения предупреждения в Visual Studio 2005. Обратите внимание, что код должен быть удаленкак есть в один cpp-файл, и он должен скомпилироваться.

Обратите внимание также на две точки в коде, которые я отметил в комментариях, которые вызывают предупреждение.Если BOOST_CLASS_EXPORT не используется, то предупреждение не вызывается, но, что более интересно, предупреждение также не срабатывает, если производный класс не использует virtual наследование!(Так что, может быть, я все-таки не понимаю C4250.)

// -- std includes --
#include <iostream>
#include <sstream>
#include <string>

// -- boost serialization --
#define BOOST_SERIALIZATION_DYN_LINK
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/export.hpp>

// Base with serialization support
struct Base
{
  virtual ~Base() {};
  virtual void DoStuff() const {
    std::cout << "Base@[" << static_cast<const void*>(this) << "]::DoStuff() called\n";
  }

  template<class Archive> // serialization support!
  void serialize(Archive & ar, const unsigned int file_version)  { /*empty*/  }
};

// (The only) Specific class with ser. support
struct Concrete2 : virtual/*!C4250!*/ public Base
{
  virtual void DoStuff() const {
    std::cout << "Concrete2@[" << static_cast<const void*>(this) << "]::DoStuff() called\n";
  }

  template<class Archive> // serialization support!
  void serialize(Archive & ar, const unsigned int ver) {
    ar & boost::serialization::base_object<Base>(*this);
    // This is just a test - no members neccessary
    std::cout << "Concrete2::serialize!" << typeid(ar).name() << "\n";
  }
};
// Without guid export -> *no* C4250, even *with* virtual inheritance
// (however, can't be serialized via base class pointer anymore)
BOOST_CLASS_EXPORT(Concrete2); 

BOOST_CLASS_TRACKING(Concrete2, boost::serialization::track_never);

int main() {
  using namespace std;
  Concrete2 obj1;
  obj1.DoStuff();

  // The following test code is not neccessary to generate the warning ...
  // (but is neccessary to show if base-pointer serialization works at runtime)
  Base* ref1 = &obj1;
  ostringstream out_buf;
  boost::archive::text_oarchive out_archive(out_buf);
  out_archive << ref1;
  const string buf = out_buf.str();

  cout << "Serialized obj:\n~~~~\n";
  cout << buf;
  cout << "\n~~~~~\n";

  std::istringstream in_buf(buf);
  boost::archive::text_iarchive in_archive(in_buf);
  // Concrete2 obj2;
  Base* ref2;
  in_archive >> ref2;
  if(ref2)
    ref2->DoStuff();
  delete ref2;
}

А вот предупреждение компилятора (тьфу!):

1>...\boost_library-1_44_0\boost\type_traits\is_virtual_base_of.hpp(61) : warning C4250: 'boost::detail::is_virtual_base_of_impl<Base,Derived,tag>::X' : inherits 'Concrete2::Concrete2::DoStuff' via dominance
1>        with
1>        [
1>            Base=type,
1>            Derived=Concrete2,
1>            tag=boost::mpl::bool_<true>
1>        ]
1>        ...\boostserializewarningtest\vbc.cpp(27) : see declaration of 'Concrete2::DoStuff'
...
1>        ...\boost_library-1_44_0\boost\mpl\eval_if.hpp(40) : see reference to class template instantiation 'boost::mpl::if_<T1,T2,T3>' being compiled
1>        with
1>        [
1>            T1=boost::is_virtual_base_of<type,Concrete2>,
1>            T2=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_virtual_base<Concrete2,type>>,
1>            T3=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_primitive<Concrete2,type>>
1>        ]
1>        ...\boost_library-1_44_0\boost\serialization\void_cast.hpp(279) : see reference to class template instantiation 'boost::mpl::eval_if<C,F1,F2>' being compiled
1>        with
1>        [
1>            C=boost::is_virtual_base_of<type,Concrete2>,
1>            F1=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_virtual_base<Concrete2,type>>,
1>            F2=boost::mpl::identity<boost::serialization::void_cast_detail::void_caster_primitive<Concrete2,type>>
1>        ]
1>        ...\boost_library-1_44_0\boost\serialization\base_object.hpp(68) : see reference to function template instantiation 'const boost::serialization::void_cast_detail::void_caster &boost::serialization::void_cast_register<Derived,Base>(const Derived *,const Base *)' being compiled
1>        with
1>        [
1>            Derived=Concrete2,
1>            Base=type
1>        ]
...    
1>        ...\boost_library-1_44_0\boost\serialization\export.hpp(128) : while compiling class template member function 'void boost::archive::detail::`anonymous-namespace'::guid_initializer<T>::export_guid(boost::mpl::false_) const'
1>        with
1>        [
1>            T=Concrete2
1>        ]
1>        ...\boostserializewarningtest\vbc.cpp(40) : see reference to class template instantiation 'boost::archive::detail::`anonymous-namespace'::guid_initializer<T>' being compiled
1>        with
1>        [
1>            T=Concrete2
1>        ]

1 Ответ

0 голосов
/ 05 ноября 2010

Причина на самом деле - проверка is_virtual_base_of от черт типа повышения.Эта контрольная конструкция сгенерирует предупреждение C4250, если проверка прошла успешно, как видно из этого примера:

...
struct base { 
    virtual void mf() { };
};
struct derived_normal : public base { 
    virtual void mf() { };
};
struct derived_virt : virtual public base { 
    virtual void mf() { };
};

int main() {
    using namespace std;

    cout << "boost::is_virtual_base_of<base, derived_normal>::value reports: ";
    // The following line DOES NOT cause C4250
    cout << boost::is_virtual_base_of<base, derived_normal>::value << endl;

    cout << "boost::is_virtual_base_of<base, derived_virt> reports: ";
    // The following line causes C4250:
    cout << boost::is_virtual_base_of<base, derived_virt>::value << endl;
 ...

FWIW, использование этого инструмента черт типа в сериализации надстроек выглядит следующим образом:

  • macro BOOST_EXPORT_CLASS ->
    • macro BOOST_CLASS_EXPORT_IMPLEMENT ->
      • struct guid_initializer (в export.hpp) ->
      • (...) void_cast.hpp / void_cast_register -> здесь используется is_virtual_base_of

Насколько я могу сказать, предупреждение совершенно безвредно в этомслучай и может быть предотвращен, обернув заголовок в:

#pragma warning( push )
#pragma warning( disable : 4250 ) // C4250 - 'class1' : inherits 'class2::member' via dominance
#include ...
#pragma warning( pop ) // C4250
...