在MacOS上调用私有构造函数时出错

时间:2018-10-10 15:47:27

标签: macos boost compiler-errors g++ clang++

我正在尝试将一些用于将给定数据编码为base64的C ++代码移植到macOS。这是来自同一来源的代码示例,该代码示例在Godbolt上编译并执行,但在我的Mac上不执行:

#include <vector>
#include <string>

#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>

using namespace boost::archive::iterators;

struct Encode
{
    /// Sets the contents of an std::string to be used
    /// as the input for the encoding operation.
    /// \param a_Data The string to encode.
    Encode(const std::string& a_Data);

    /// Evaluates the expression and performs the base64 encoding.
    /// \returns Base64 encoded string.
    operator std::string() const;
private:
    std::string Evaluate() const;

    const char * m_Data;
    size_t m_Size;
    bool m_LineBreaks;
};

Encode::Encode(const std::string& a_Data)
    : m_Data(a_Data.c_str())
    , m_Size(a_Data.size())
    , m_LineBreaks(false)
{
}

std::string Encode::Evaluate() const
{
    typedef base64_from_binary<
            transform_width<std::string::const_iterator,6,8>
        > iterator;

    typedef insert_linebreaks<iterator, 72> linebreak_iterator;

    std::string base64;
    if (m_LineBreaks) {
        base64.assign(
            linebreak_iterator(m_Data),
            linebreak_iterator(m_Data + m_Size));
    }
    else {
        base64.assign(
            iterator(m_Data),
            iterator(m_Data + m_Size));
    }

    return base64;
}
Encode::operator std::string() const
{
    return Evaluate();
}

int main()
{
    const std::string str64 = Encode("Hello World");    
    return 0;
}

我正在使用g ++进行编译:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.2)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

但是,第iterator(m_Data),行出现以下错误:

boost/archive/iterators/transform_width.hpp:112:17: error: calling a private constructor of class 'std::__1::__wrap_iter<const char *>'
        super_t(Base(static_cast< T >(start))),
                ^
<build_path>/boost/archive/iterators/base64_from_binary.hpp:91:13: note: in instantiation of function template specialization 'boost::archive::iterators::transform_width<std::__1::__wrap_iter<const char *>, 6, 8,
      char>::transform_width<const char *>' requested here
            Base(static_cast< T >(start)),
            ^
<src_path>/utilsBase64.cc:105:13: note: in instantiation of function template specialization 'boost::archive::iterators::base64_from_binary<boost::archive::iterators::transform_width<std::__1::__wrap_iter<const char *>, 6, 8, char>, char>::base64_from_binary<const
      char *>' requested here
            iterator(m_Data),
            ^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iterator:1420:31: note: declared private here
    _LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT_DEBUG : __i(__x) {}
                              ^

我尝试使用clang ++并尝试将-stdlib=libc++stdlib=libstdc++设置为类似结果。使用代码中显示的base64_from_binary的所有示例都类似于给定的代码示例。

关于m_Data的另一个细节,它是一个声明为:const char * m_Data;

的成员变量。

有人可以解释一下如何解决吗?

1 个答案:

答案 0 :(得分:0)

将代码修改为以下内容有助于解决该问题:

std::string Encode::Evaluate() const
{
    typedef base64_from_binary<
            transform_width<std::string::const_iterator,6,8>
        > iterator;

    typedef insert_linebreaks<iterator, 72> linebreak_iterator;

    std::string data(m_Data, m_Size);
    std::string base64;
    if (m_LineBreaks) {
        base64.assign(
            linebreak_iterator(data.begin()),
            linebreak_iterator(data.end()));
    }
    else {
        base64.assign(
            iterator(data.begin()),
            iterator(data.end()));
    }

    return base64;
}