热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C++03标准下,如何在模板类的构造函数中初始化数组长度是模板参数,数组元素没有无参构造函数的数组成员?

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include

#include

#include

#include

#include

#include

using namespace std;



enum {

    E_alice,

    E_bob,

};



class T_key {

private:

    int32_t id;

    union {

        uint32_t case1;

        int32_t case2;

    } data;



public:

    //T_key() {} //uncomment this, the compilation will be fine.

    virtual ~T_key(){}

    T_key(int32_t id_) : id(id_){ memset(&data, 0, sizeof(data));}

    T_key(const T_key &rhs) : id(rhs.id) { memcpy(&data, &rhs.data, sizeof(data)); }

    const T_key& self() const {return *this;}



    void set_case1(uint32_t rhs) { data.case1 = rhs; id = E_alice;}

    void set_case2(int32_t rhs) { data.case2 = rhs; id = E_bob;}

    uint32_t get_case1() const {return data.case1;}

    int32_t get_case2() const {return data.case2;}



    int32_t compare(const T_key &rhs) const {

        int32_t ret = id - rhs.id;

        if (ret == 0) {

            ret = memcmp(&data, &rhs.data, sizeof(data));

        }

        return ret;

    }

};



template

class multipleKeySet{

public:

    T_key set[N];

    multipleKeySet(){} //!!compile error

    virtual ~multipleKeySet(){}

    bool operator<(const multipleKeySet &rhs) const {

        int32_t ret = 0;

        for (uint32_t idx = 0; idx
            ret += set[idx].compare(rhs.set[idx]);

            if (ret != 0) {

                break;

            }

        }

        return ret;

    }

};



template

class multipleKeyMap : protected std::map, T>{

public:

    multipleKeyMap(){}

    virtual ~multipleKeyMap(){}

    void beginIter(typename std::map, T>::const_iterator &iter_in) const {

        iter_in = std::map, T>::begin();

    }

    bool isEndIter(typename std::map, T>::const_iterator &iter_in) const {

        return iter_in == std::map, T>::end();

    }

    void nextIter(typename std::map, T>::const_iterator &iter_in) const {

        iter_in++;

    }

    uint32_t getSize() const {return std::map, T>::size();}

};



class keyA : protected T_key {

public:

    keyA(int32_t id = E_alice):T_key(id){}

    virtual ~keyA(){}

    const T_key& get_self() const { return self();}

    void set_value(uint32_t c1) { set_case1(c1); }

    uint32_t get_value() const { return get_case1(); }

};



class keyB : protected T_key {

public:

    keyB(int32_t id = E_bob):T_key(id){}

    virtual ~keyB(){}

    const T_key& get_self() const { return self();}

    void set_value(int32_t c2) { set_case2(c2); }

    int32_t get_value() const { return get_case2(); }

};



class MKVpair {

private:

    keyA alice_m;

    keyB bob_m;

    void* datas;

public:

    MKVpair() : datas(NULL){}

    virtual ~MKVpair(){}

    const keyA & get_alice() const {return alice_m;}

    keyA & get_alice() {return alice_m;}

    const keyB & get_bob() const {return bob_m;}

    keyB & get_bob() {return bob_m;}

};



class MKVmap : public multipleKeyMap<2, MKVpair> {

public:

    MKVmap(){}

    virtual ~MKVmap(){}

    void insertPair(const MKVpair &rhs) {

        multipleKeySet<2> keyset;

        keyset.set[0] = rhs.get_alice().get_self();

        keyset.set[1] = rhs.get_bob().get_self();

        insert(std::make_pair(keyset, rhs)); // seems issue comes from this stmt

    }



    typedef multipleKeyMap<2, MKVpair>::const_iterator const_pair_iter;

    bool findPair(const keyA& k1, const keyB& k2, const_pair_iter &ret) {

        multipleKeySet<2> keyset;

        keyset.set[0] = k1.get_self();

        keyset.set[1] = k2.get_self();

        ret = find(keyset);

        return ret != end();

    }

};



int main() {

#if 1

    MKVmap test;

    // 1

    MKVpair p1;

    p1.get_alice().set_value(233);

    p1.get_bob().set_value(-233);

    // 2 (duplicate

    MKVpair p2;

    p2.get_alice().set_value(233);

    p2.get_bob().set_value(-233);

    // 3

    MKVpair p3;

    p3.get_alice().set_value(123);

    p3.get_bob().set_value(-123);



    test.insertPair(p1);

    test.insertPair(p2);

    test.insertPair(p3);

    cout <


    MKVmap::const_pair_iter it;

    for (test.beginIter(it); !test.isEndIter(it); test.nextIter(it)) {

        cout <second.get_alice().get_value() <
        cout <second.get_bob().get_value() <
    }

#endif

    return 0;

}

折腾了一天没找到别的方法,感觉某处代码的用法上违背了书写它们语法的本意,想不明白





   



推荐阅读
author-avatar
dudumama2011
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有