【发布时间】:2016-03-02 14:55:12
【问题描述】:
我已经尝试了四种不同的方法来让它发挥作用。我想在调用“get_all”时分配变量来捕获接口输入。
1 - 由于我对模板缺乏了解而失败
#include <iostream>
#include <string>
#include <map>
using namespace std;
#include <boost/lexical_cast.hpp>
class interface {
template <typename t> struct val_t{ t val; t& out; };
map<string, val_t> KeyDict;
template <typename t> void add(t& out, string key, t value) {
KeyDict[key] ={ value, out };
}
template <typename t> void get_all() {
for (auto i = KeyDict.begin(); i != KeyDict.end(); ++i) {
i->second().out = boost::lexical_cast<t>(i.second().val);
}
}
};
int main() {
int i;
double d;
string s;
bool b;
interface ui;
ui.add(i, "my int", 1); // if it's not possible do to 1 here, I could do "1";
ui.add(d, "my double", 3.14);
ui.add(s, "my string", "good stuff");
ui.add(b, "my bool", false);
ui.get_all();
cout <<
"int = " << i << endl <<
"double = " << d << endl <<
"string = " << s << endl <<
"bool = " << b << endl;
}
2 - 由于某种原因 add() 不喜欢字符串
#include <iostream>
#include <string>
#include <map>
#include <boost/any.hpp>
using namespace std;
class interface {
public:
struct any { boost::any out; boost::any val; };
map<string, any> KeyDict;
template <typename t> void add(t& out, string key, t defval) { KeyDict[key].out = defval; KeyDict[key].val = defval; }
void get_all() {
for (auto i = KeyDict.begin(); i != KeyDict.end(); ++i)
i->second.out = i->second.val;
}
};
int main() {
int i = 0;
double d = 0;
string s = "";
bool b = 0;
interface ui;
ui.add(i, "my int", 1); // if it's not possible do to 1 here, I could do "1";
ui.add(d, "my double", 3.14);
ui.add(s, "my string", "good stuff");
ui.add(b, "my bool", false);
ui.get_all();
cout <<
"int = " << i << endl <<
"double = " << d << endl <<
"string = " << s << endl <<
"bool = " << b << endl;
cin.get();
}
3 - 错误:试图引用已删除的函数
#include <iostream>
#include <string>
#include <map>
using namespace std;
class interface {
public:
struct typer {
int& i_out;
double& d_out;
bool& b_out;
string& s_out;
int i = 0;
double d = 0;
bool b = 0;
string s;
char t = 0;
};
map<string, typer> KeyDict_i;
map<string, typer> KeyDict_d;
map<string, typer> KeyDict_b;
map<string, typer> KeyDict_s;
void add(int& out, string key, int val) { KeyDict_i[key].i_out = out; KeyDict_i[key].i = val; KeyDict_i[key].t = 'i'; }
void add(double& out, string key, double val) { KeyDict_d[key].d_out = out; KeyDict_d[key].d = val; KeyDict_d[key].t = 'd'; }
void add(bool& out, string key, bool val) { KeyDict_b[key].b_out = out; KeyDict_b[key].b = val; KeyDict_b[key].t = 'b'; }
void add(string& out, string key, string val) { KeyDict_s[key].s_out = out; KeyDict_s[key].s = val; KeyDict_s[key].t = 's'; }
void get_all() {
for (auto i = KeyDict_i.begin(); i != KeyDict_i.end(); ++i) i->second.i_out = i->second.i;
for (auto i = KeyDict_d.begin(); i != KeyDict_d.end(); ++i) i->second.d_out = i->second.d;
for (auto i = KeyDict_b.begin(); i != KeyDict_b.end(); ++i) i->second.b_out = i->second.b;
for (auto i = KeyDict_s.begin(); i != KeyDict_s.end(); ++i) i->second.s_out = i->second.s;
}
};
int main() {
int i = 0;
double d = 0;
string s = "";
bool b = 0;
interface ui;
ui.add(i, "my int", 1); // if it's not possible do to 1 here, I could do "1";
ui.add(d, "my double", 3.14);
ui.add(s, "my string", "good stuff");
ui.add(b, "my bool", false);
ui.get_all();
cout <<
"int = " << i << endl <<
"double = " << d << endl <<
"string = " << s << endl <<
"bool = " << b << endl;
}
#3 是最直接的,因为我没有使用任何增强或模板。也许这将是最容易从我已经拥有的东西开始工作?
【问题讨论】:
-
在第 1 节中:
std::map需要 type 参数。但val_t不是类型(它是模板)。 -
对于
add,您需要了解模板参数推导(并且可能将您的参数之一设为非推导类型)。 -
@ElanHickler 为什么不让整个类成为模板而不是模板化各个函数?
标签: c++ dictionary casting key-value getter-setter