Jan*_*dec 8 visual-c++-2008 c++03
我需要从绑定成员函数创建一个谓词,所以我将它包装在一个boost::function<bool(SomeObject const &)>.这看起来很好,但是我还需要在一个案例中否定它.然而
boost::function<bool(SomeObject const &)> pred;
std::not1(pred);
Run Code Online (Sandbox Code Playgroud)
不能在MSVC++ 9.0(Visual Studio 2008)下编译,抱怨对引用的引用无效:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\functional(213) : warning C4181: qualifier applied to reference type; ignored
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\functional(213) : error C2529: '_Left' : reference to reference is illegal
Run Code Online (Sandbox Code Playgroud)
问题是boost::function定义argument_typeas SomeObject const &和std::unary_negate<_Fn1>实例化由std::not1内部尝试使用const typename _Fn1::argument_type&和编译器拒绝它因为T::argument_type已经是一个引用.我确信这应该在C++ 11下编译,但这只是旧的编译器,只是C++ 03.所以我想知道它是谁的错:
unary_negate了const typename Predicate::argument_type& x参数),argument_type即使实际参数是或者,也不应该引用boost::function不应该与参考参数一起使用?故障肯定不是Boost的;boost::function基本上只是std::function,具有相同的语义。带有boost::function参考参数的 s 也可以正常工作。你只是不能将它们与std::not1其他东西一起使用<functional>。
C++11 的引用折叠可以std::not1按照您认为应该的方式工作。如果没有引用折叠 \xe2\x80\x94,C++03 中指定的方式std::not1就不可能工作,除非实现者做了一点创造性的解释,而不是盲目地遵循标准的字母。
通过添加带有引用的 for 谓词的特化,可以在 C++03 中工作,但 libc++ 和 libstdc++ 都没有这样做。std::not1std::unary_negateargument_type
但你知道谁有吗?促进!如果您只是更改代码以boost::not1在当前使用的任何地方使用std::not1,则一切都会正常工作。基本上,将boost命名空间视为 ; 的 C++11 兼容版本std。任何在 C++11 命名空间中工作的东西std都可能在 C++03boost命名空间中工作。
警告,希望题外话:我的 Macbook ( Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)) 上的 Clang 编译器即使在模式下也会默默地折叠引用-std=c++03,因此
typedef const int& ref;\ntypedef const ref& ref2;\nRun Code Online (Sandbox Code Playgroud)\n\n不会产生任何错误。当您测试 C++03 代码时,请确保您没有使用具有此错误功能的编译器。
\n