boost addable和dll的可访问性错误

Accessibility error with boost addable and dll

本文关键字:访问 错误 addable dll boost      更新时间:2023-10-16

我正在使用Visual Studio 2008,在Windows Vista上,并将函数转换为构建调试DLL。

编译器错误:

我得到一个可访问性错误与boost::操作符模板:

error C2248: 'Field::Integer::Integer' : cannot access protected member declared in class 'Field::Integer'  
c:program filesboostboost_1_52_0boostoperators.hpp(257) : while compiling class template member function 'Field::Integer boost::operator +(Field::Integer,const Field::Integer &)'
1>        c:program filesboostboost_1_52_0boostoperators.hpp(836) : see reference to class template instantiation 'boost::addable1<T,B>' being compiled
1>        with
1>        [
1>            T=Field::Integer,
1>            B=boost::detail::empty_base<Field::Integer>
1>        ]
1>        see reference to class template instantiation 'boost::addable<T>' being compiled
1>        with
1>        [
1>            T=Field::Integer
1>        ]
1>        see reference to class template instantiation Field::Numeric<Value_Type,Descendant_Class>' being compiled
1>        with
1>        [
1>            Value_Type=int,
1>            Descendant_Class=Field::Integer
1>        ]

代码(简化为基本语句):

#ifndef FIELD_INTEGER_HPP
#define FIELD_INTEGER_HPP
#ifdef FIELD_EXPORTS
#define FIELD_API __declspec(dllexport)
#else
#define FIELD_API __declspec(dllimport)
#endif
#include "boost/operators.hpp"
namespace Field
{
template <class Value_Type, class FIELD_API Descendant_Class>
class FIELD_API Numeric
    : public boost::addable<Descendant_Class>,
      public boost::subtractable<Descendant_Class>,
      public boost::multipliable<Descendant_Class>,
      public boost::dividable<Descendant_Class>
{
  public:
                                Numeric(const Value_Type&   new_value = 0);
                                Numeric(const Numeric& fn);
    virtual                     ~Numeric();
    Descendant_Class            operator+=(const Descendant_Class& dc);
    Descendant_Class            operator-=(const Descendant_Class& dc);
    Descendant_Class            operator*=(const Descendant_Class& dc);
    Descendant_Class            operator/=(const Descendant_Class& dc);
    void                        clear_field(void);
    bool                        supports_value_as_string(void) const;
};

class FIELD_API Integer
    : public Field::Numeric<int, Field::Integer>
{
  public:
        //! Destructor
    virtual                     ~Integer();
  protected:
    //! Constructor
                                Integer(const int               new_value);
    //! Copy constructor
                                Integer(const Integer& fui);
};
} // End namespace Field
#endif  // FIELD_INTEGER_HPP

我的目标是把上面的代码变成一个可导出的调试DLL或发布DLL。
生成的代码在静态库设置中没有错误。

问题:

在上面的代码中,什么是必要的修改,使其成为调试或发布DLL (Visual Studio 2008, Windows Vista, 32位)?

我搜索了web和StackOverflow,我只得到使用模板的结果,而不是传递类作为模板参数和dll。

由于Numeric继承了boost::addable(以及3个相关的类)而发生错误。这将生成一个签名为

的非成员函数operator+
Field::Integer boost::operator +(Field::Integer,const Field::Integer &)

按值接受左实参的原因是为了优化右值引用和复制省略。这需要boost::operator+访问Integer的复制构造函数,即protected,因此您会得到一个错误。我不明白为什么作为静态库编译对你有用,而DLL却不行。

对于这样的访问问题,建议使用复制构造函数public。对我来说,不希望Integer作为叶子类似乎是一个设计错误,因为如果这真的是你想要的,为什么你仍然希望能够使用它进行加法和其他算术运算?

另一种选择是授予boost::operator+(Integer, Integer const&)友谊。不推荐使用友谊路由,因为它依赖于boost::addable的实现。通常情况下,您会将友谊授予类boost::addable,但它的实现将使用非成员朋友函数operator+而不是成员函数operator+。您不应该让自己的类Integer依赖于这样的实现细节。