c++ - boost::any 测试代码可以使用 Sun CC 编译,但不能使用 g++

标签 c++ compiler-construction boost g++ sun

以下noddy测试代码:

#include <iostream>
#include <list>
#include <boost/any.hpp>
#include <boost/foreach.hpp>
#include <typeinfo.h>

using boost::any_cast;
using std::cout;
using std::cerr;
typedef std::list<boost::any> many;

template <typename T>
inline bool is_any(const boost::any& op)
{
  return (op.type() == typeid(T));
}

int main()
{
  many theStrangeList;
  theStrangeList.push_back("Can you really...");
  theStrangeList.push_back(std::string ("do random types in 1 container?"));
  theStrangeList.push_back(6.359);
  theStrangeList.push_back(7);

  BOOST_FOREACH(boost::any a, theStrangeList)
    {
      try
        {
          if (is_any<const char*>(a))
            {
              cout << any_cast<const char*>(a) << '\n';
            }
          else if (is_any<std::string>(a))
            {
              cout << any_cast<std::string>(a) << '\n';
            }
          else if (is_any<double>(a))
            {
              cout << "double = " << any_cast<double>(a) << '\n';
            }

        }
      catch (const boost::bad_any_cast& e)
        {
          cerr << e.what();
          cerr << "\n";
        }
    }


  return 0;
}

使用 Sun 的 CC 编译器和默认设置编译并运行良好。 但是,当使用 g++ 时,我得到以下信息:

$ g++ -I$BOOST_ROOT -o myany myany.cpp
myany.cpp:5:22: typeinfo.h: No such file or directory
/ilx/boost_1_41_0/boost/any.hpp: In constructor `boost::any::holder<ValueType>::holder(const ValueType&) [with ValueType = char[18]]':
/ilx/boost_1_41_0/boost/any.hpp:47:   instantiated from `boost::any::any(const ValueType&) [with ValueType = char[18]]'
myany.cpp:21:   instantiated from here
/ilx/boost_1_41_0/boost/any.hpp:122: error: ISO C++ forbids assignment of arrays

这是 g++ 版本 3.4.3,因此在 4.x 版本上可能会有所不同,我稍后再试。这是 boost any 中没有包含“is_any”模板的原因,还是编译器错误?

如果我删除模板,我会得到相同的结果,正如您对内联函数所期望的那样。

(related question)

最佳答案

对于第一个错误尝试

#include <typeinfo>

不是

#include <typeinfo.h>

关于c++ - boost::any 测试代码可以使用 Sun CC 编译,但不能使用 g++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1832504/

相关文章:

c++ - CMake 与 Poky 交叉编译

compiler-construction - 在没有 Gforth 的情况下编译 Gforth?

c++ - 如何修复 Turbo C++ 错误 "Cannot open include file: graphics.h: no such files or director"

boost - boost::asio tcp 套接字关闭是否阻塞?

c++ - 命名空间内的类友元函数

c++ - 在 C++ 中搜索 CString

c++ - g++ 链接与 Visual C++ 链接

c++ - 阴影贴图OpenGL阴影并不总是绘制,并且绘制光源位置的位置

生成 .exe 文件的 Java 编译器选项

c++ - Windows 上的 Boost::Asio 不能异步工作