gpt4 book ai didi

templates - 模板中标记为 boost::bimap - 它们有效吗?

转载 作者:行者123 更新时间:2023-12-02 22:49:31 25 4
gpt4 key购买 nike

我正在模板化类中嵌入 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 );
}
};

我想将标签封装在模板类中,因为不需要在类之外知道它们。理想情况下,它们应该是私有(private)的,但为了避免访问权限产生任何问题,它们被声明为公共(public)的。

示例 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;

同样,通过标签访问映射的两行都会产生上述编译错误。在重写代码以使用“左”和“右” View 之前,如果有人能帮助我理解问题 2:为什么不能在标记类型的定义中使用模板参数?

感谢您的所有意见!

最佳答案

第二种情况需要template资格。原因是 Label1Label2 现在是从属名称。

auto ByAddress = TheBimap.template by<Label1>().find("TheGivenAdddress");
auto ByNumber = TheBimap.template by<Label2>().find(AnAddress);

参见Where and why do I have to put the "template" and "typename" keywords?

Question 2: Why is it not possible to use a template parameter in the definition of the tagged type?

同理。您可能还需要 typename 资格。

演示

Live On Coliru

#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;

namespace bm = boost::bimaps;

template <class T>
class Test {
private:
typedef bm::bimap<
bm::unordered_set_of<bm::tagged<T, struct key_idx> >,
bm::set_of <bm::tagged<Address, struct value_ix> >
> BidirectionalMap;

typedef typename BidirectionalMap::value_type Record;

BidirectionalMap _theBimap;

public:
void aFunction(const Address &anAddress, T const& parameters)
{
auto newRecord = _theBimap.insert(Record("TheGivenAdddress", anAddress));
auto byNumber = _theBimap.template by<value_ix>().find(anAddress);
auto byAddress = _theBimap.template by<key_idx>().find(parameters);

(void) newRecord, (void) byAddress, (void) byNumber;
}
};

int main() {
Test<std::string> t;
t.aFunction(3.14, "hello");
}

要真正将标记类型保留在类添加中

    struct key_idx;
struct value_idx;

(无需定义它们)。查看live

关于templates - 模板中标记为 boost::bimap - 它们有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704621/

25 4 0