创建命名空间类以在另一个类中定义容器
creating namespace class to define containers in another class
我想知道我在哪里可以做这样的事情(c++11):
//class FOO is a static class which contains static members and lots of typedefs for
container types such as vector, map, BOOST Graph (adjacency list), property_maps etc
current implementation:
// file: traits.hpp
class FOO {
typedef std::vector<int> container_t;
typedef std::map<int, int> map_t;
typedef boost::adjacency_list < > graph_t;
typedef boost::property_map < > p_map_t // syntax is not correct though(just for understanding purpose)
// lots of other typedefs ...
static int var1;
static int var2; // lots of other static variables
};
int FOO::var1 = 10;
int FOO::var2 = 20; //initialize static variables
// file BAR.hpp
template <class FOO>
class BAR {
public:
typedef typename FOO::map_t map_t;
typedef typename FOO::container_t conatiner_t; // and so on
typedef typename FOO::graph_t graph_t
BAR () { ... constructor ... }
private:
map_t my_map;
container_t my_container;
graph_t my_Graph;
};
Problem in above: lots of typedef typename need to be used always. Note that I
could directly use this in private: e.g.
typename FOO::map_t my_map;
typename FOO::graph_t my_graph;
but am avoiding it as there are lots of typenames and stuff which i will have
to manage and the code
//What I want to do :-->
// file: traits.hpp
// create a namespace "traits"
namespace traits {
class FOO {
typedef std::vector<int> container_t;
typedef std::map<int, int> map_t;
typedef boost::adjacency_list < > graph_t;
// lots of other typedefs
static int var1;
static int var2; // lots of other static variables
};
int FOO::var1 = 10;
int FOO::var2 = 20; //initialize static variables
}; // end namespace
// file BAR.hpp
using namespace traits;
class BAR {
public:
template <..>
void bar () // functions
private:
FOO::map_t my_map;
FOO::container_t my_container;
FOO::graph_t my_Graph;
}
OR would be like this :
private :
traits::FOO::map_t my_map;
traits::graph_t my_graph;
...
Cant I directly use like this as FOO will be the only class in namespace traits?
traits::map_t my_map;
traits::graph_t my_graph; //and so on...
我的主要动机是保持所有的类型和静态变量在一个类(FOO),并包装它在一个命名空间(特征),这样我就不必显式地传递类FOO作为模板参数类BAR,然后再次使用类型,这不会使代码看起来很好,因为我有很多其他类使用FOO容器。同时,我需要将容器类型定义和静态变量封装在一个类中。
有人能帮我一下吗?谢谢! !为traits创建一个自定义命名空间:
namespace traits
{
typedef std::vector<int> container_t;
typedef std::map<int, int> map_t;
typedef boost::adjacency_list < > graph_t;
typedef boost::property_map < > p_map_t // syntax is not correct though(just for understanding purpose)
};
class BAR {
public:
template <..>
void bar () // functions
private:
traits::map_t my_map;
traits::container_t my_container;
traits::graph_t my_Graph;
....
};
相关文章:
- 在命名空间中定义函数还是限定函数
- 使用命名空间时出现多个定义错误
- 如何对命名空间限定类型进行类型定义?
- C++未定义的命名空间
- 命名空间更改函数定义
- 使用命名空间在另一个".cpp"文件中定义
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 在方法定义中显式指定命名空间
- 在 c++ 中定义命名空间中模板类的友元函数时出现问题
- 与自定义命名空间一起使用时实现 std::error_category、名称解析问题
- 为什么 ADL 优先于'std namespace'中的函数,但等于用户定义命名空间中的函数?
- G++显示关于未定义命名空间的错误
- 定义命名空间内类的构造函数
- 在自定义命名空间下包含一个标头
- 与自定义命名空间的链接失败未解析的外部符号
- 自定义命名空间的链接器错误
- 试图通过using-declaration定义命名空间成员
- 如何正确重载作为用户定义命名空间一部分的模板化类的插入运算符?
- 定义命名空间的变体和__visibility__的含义
- 重新定义命名空间的变量