1

我正在尝试根据 Boost::phoenix 文档从模板函数创建一个惰性函数。代码看起来像这样

#include <iostream>

#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>

using namespace boost;
using namespace boost::phoenix;

namespace demo
{
     bool func(double a,double b)
     {
         return bool(a > b);
     }
}

BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)

int main(int argc,char **argv)
{
     namespace pl = boost::phoenix::placeholders;
     auto comperator = func(pl::arg1,pl::arg2);
     std::cout<<comperator(1.2,12.4)<<std::endl;
     std::cout<<comperator(0.5,0.1)<<std::endl;
}

这实际上是 BOOST 文档中的示例之一。将此文件存储为 mk_lazy1.cpp 并尝试编译给出

$ g++ -omk_lazy1 mk_lazy1.cpp                 
mk_lazy1.cpp:26:1: error: template argument 1 is invalid      
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token  
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’ 
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type    
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope   

我在 Debian 测试系统上使用 gcc-4.7。老实说,我有点迷茫,因为我完全不知道这里出了什么问题(正如我所说,这实际上是 Boost 文档提供的示例之一的逐字复制)。

有人有好主意吗?

4

1 回答 1

3

Remove using namespaces and all will work fine. Or write using namespaces AFTER adapt macro and all will work fine too. Or put macro into unnamed namespace.

于 2013-04-26T10:45:27.727 回答