我在模板类中嵌入了 boost::bimap,经过多次反复试验,我发现了一些可以编译的东西和一些不能编译的东西。我正在使用 g++ (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6) 和 Boost 1.55。我将给出示例 1 中的完整代码,仅给出示例 2 和示例 3 的更改部分。该函数没有做任何有用的事情,只是在这里测试语法和编译器。
编译的示例 1(完整列表):
#include <string>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/set_of.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/tags/tagged.hpp>
typedef double Address;
struct Label1 {};
struct Label2 {};
template< class TemplateParameter >
class Test
{
private:
typedef boost::bimaps::tagged< std::string, Label1 > KeyType;
typedef boost::bimaps::tagged< Address, Label2 > ValueType;
// No changes after this line in Example 2 or Example 3
typedef boost::bimaps::unordered_set_of< KeyType > KeySet;
typedef boost::bimaps::set_of< ValueType > ValueSet;
typedef boost::bimaps::bimap < KeySet, ValueSet > BidirectionalMap;
typedef typename BidirectionalMap::value_type Record;
// Create the bimap
BidirectionalMap TheBimap;
void aFunction ( const Address & AnAddress,
const TemplateParameter & Parameters )
{
auto NewRecord = TheBimap.insert( Record( "TheGivenAdddress", AnAddress ) );
auto ByAddress = TheBimap.by< Label1 >().find( "TheGivenAdddress" );
auto ByNumber = TheBimap.by< Label2 >().find( AnAddress );
}
};
我想将标签封装在模板类中,因为不需要在类之外知道它们。理想情况下,它们应该是私有的,但为了避免访问权限产生任何问题,它们被声明为公共的。
不编译的示例 2(除外):
typedef double Address;
template< class TemplateParameter >
class Test
{
public:
struct Label1 {};
struct Label2 {};
private:
typedef boost::bimaps::tagged< std::string, Label1 > KeyType;
typedef boost::bimaps::tagged< Address, Label2 > ValueType;
标签访问 bimap 的两行都会产生以下消息:
error: expected primary-expression before ‘>’ token
auto ByAddress = TheBimap.by< Label1 >().find( "TheGivenAdddress" );
这可以理解为 Label1 需要完全限定,从成为模板类的一部分,如下所示。
auto ByAddress = TheBimap.by< Test<TemplateParameter>::Label1 >().find( "TheGivenAdddress" );
然而,产生了同样的错误。问题1:有人明白为什么吗?
使用模板类 Test 的真正问题和原因是我希望 Label 1 的类型是模板参数。因此,回到示例 1 并仅替换std::string
为模板参数。
不编译的示例 3(除外):
typedef double Address;
struct Label1 {};
struct Label2 {};
template< class TemplateParameter >
class Test
{
private:
typedef boost::bimaps::tagged< TemplateParameter, Label1 > KeyType;
typedef boost::bimaps::tagged< Address, Label2 > ValueType;
同样,通过标签访问地图的两行都会产生上述编译错误。在重写代码以使用“左”和“右”视图之前,如果有人能帮助我理解问题2:为什么不能在标记类型的定义中使用模板参数?
感谢所有输入!