使用命名空间和标题

时间:2013-04-25 10:34:37

标签: c++ c++98

我正在开展一个项目,我在头文件中写了几乎所有的测试代码。我之所以这样做主要是因为我正在进行测试驱动开发,这导致我添加的每个类都有大量的补充类:Interface,Test,Mock等。我想我会发疯如果我还要处理所有这些文件的cpp版本......

我没有使用命名空间std"添加"我的标题开始,因为我已经知道这是一个不,不。无论如何,假设我当前在测试开始时初始化我的Blob对象,如下所示:

Blob v =
    boost::assign::list_of(std::pair<std::string, Container >("Scotland",Container(boost::assign::list_of(1)(2)(3).convert_to_container<std::vector<int> >())))
    (std::pair<std::string, Container >("Sweden",Container()));

其中Blob typedef在某处std::vector<std::pair<std::string, Container > >

我怎样才能让这个更漂亮?我使用list_of的原因是为了让事情更具可读性,但在这种情况下,我认为它使阅读起来更加困难。这要好得多:

Blob v =
    list_of(pair<string, Container >("Scotland",Container(list_of(1)(2)(3).convert_to_container<vector<int> >())))
    (pair<string, Container >("Sweden",Container()));

但我无法在标题中执行此操作...

我该怎么做才能解决这个问题?我正在使用C ++ 98。

更新

只是一个想法。如果我将所有测试标题重命名为cpp文件,该怎么办?

1 个答案:

答案 0 :(得分:1)

TDD需要短编辑 - >编译 - >运行周期时间。因此,您应该在cpp文件中编写尽可能多的代码以减少编译时间。 不过,您可以使用init函数解决问题:

inline Blob InitBlob()
{
    using namespace boost;
    using namespace std;
    return assign::list_of(/*...*/);
}

Blob v = InitBlob();