4

我这里有一个奇怪的情况。我正在尝试将 Botan 加密库与 VS2015 一起使用(因为项目的其他一些部分使用了一些 VS2013 无法编译的繁重 C++11 代码)并且我得到了一个很长的编译错误(见下文)。

在尝试了各种方法之后,我得出的结论是,即使编译后的 c++ 源文件中包含了其中一个 botan 头文件,编译器也会抛出以下错误。现在我在文件中有一行:

#include <botan/botan.h>

这是我得到的错误:

    C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\xmemory0(876): error C2664: 'Botan::secure_allocator<_Newfirst>::secure_allocator(const Botan::secure_allocator<_Newfirst> &)': cannot convert
argument 1 from 'std::_Wrap_alloc<Botan::secure_allocator<Botan::byte>>' to 'const Botan::secure_allocator<_Newfirst> &'
        with
        [
            _Newfirst=std::_Container_proxy
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\xmemory0(876): note: Reason: cannot convert from 'std::_Wrap_alloc<Botan::secure_allocator<Botan::byte>>' to 'const Botan::secure_allocator<_Ne
wfirst>'
        with
        [
            _Newfirst=std::_Container_proxy
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\xmemory0(876): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(587): note: see reference to function template instantiation 'std::_Wrap_alloc<Botan::secure_allocator<_Newfirst>>::_Wrap_alloc<std::_Wr
ap_alloc<Botan::secure_allocator<Botan::byte>>>(_Other &) noexcept' being compiled
        with
        [
            _Newfirst=std::_Container_proxy,
            _Other=std::_Wrap_alloc<Botan::secure_allocator<Botan::byte>>
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(587): note: see reference to function template instantiation 'std::_Wrap_alloc<Botan::secure_allocator<_Newfirst>>::_Wrap_alloc<std::_Wr
ap_alloc<Botan::secure_allocator<Botan::byte>>>(_Other &) noexcept' being compiled
        with
        [
            _Newfirst=std::_Container_proxy,
            _Other=std::_Wrap_alloc<Botan::secure_allocator<Botan::byte>>
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(585): note: while compiling class template member function 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(void)
'
        with
        [
            _Ty=Botan::byte,
            _Alloc=Botan::secure_allocator<Botan::byte>
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(552): note: see reference to function template instantiation 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(voi
d)' being compiled
        with
        [
            _Ty=Botan::byte,
            _Alloc=Botan::secure_allocator<Botan::byte>
        ]
C:\Program Files\Microsoft Visual Studio 14.0\VC\INCLUDE\vector(679): note: see reference to class template instantiation 'std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>' being compiled
        with
        [
            _Ty=Botan::byte,
            _Alloc=Botan::secure_allocator<Botan::byte>
        ]
c:\Botan\include\botan-1.11\botan/rng.h(43): note: see reference to class template instantiation 'std::vector<Botan::byte,Botan::secure_allocator<Botan::byte>>' being compiled
NMAKE : fatal error U1077: 'C:\PROGRA~1\MICROS~3.0\VC\bin\cl.exe' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 14.0\VC\BIN\nmake.exe"' : return code '0x2'
Stop.

由于我能够编译和运行植物测试,我觉得我错过了一些东西,但我不知道是什么。有没有人有这方面的经验?(顺便说一句:相同的代码可以很好地与 g++ 4.9 编译)

4

1 回答 1

6

查看来源,似乎Botan::secure_allocator没有提供表单的模板构造函数

template<class U> secure_allocator(const secure_allocator<U>& other);

这是标准要求的。在当前的工作草案N4527中,相关位在 [17.6.3.5] 表 28 - 分配器要求中;第 9 段中的例子也很有用。

因此,我们不能责怪 VC 14 附带的标准库实现需要它才能编译。在我看来,错误在 Botan 一方。


一个快速的解决方法是将这样的定义添加到Botan::secure_allocator

template<class U> secure_allocator(const secure_allocator<U>&) BOTAN_NOEXCEPT { }

由于这个分配器模板的实例没有任何非静态数据成员,一个空的主体应该没问题。但是,我对库不熟悉,我们在这里讨论的是密码学,所以,在使用它做任何严肃的事情之前,请与库作者确认更改。


另一种可能的解决方法:

我注意到调用混合类型构造函数的代码似乎只在启用迭代器调试时才启用,这在调试模式下默认发生。

您是否尝试过在发布模式下编译?如果我的观察是正确的,您将不会再收到此错误,因为用于迭代器调试的附加机制将被禁用。

要在调试模式下获得相同的行为,请将_ITERATOR_DEBUG_LEVEL宏设置为0全局。

调试迭代器可用于检测错误(只要性能影响不会影响您),因此我不会将其用作永久修复,但如果您不想这样做,它可以用作临时解决方法修改Botan头文件。

这也可以解释为什么您能够编译测试:也许它们是在发布模式下编译的,或者无论如何,使用禁用迭代器调试的设置组合?

于 2015-08-04T09:36:38.993 回答