我一直在玩基于 Boostmap_list_of
模板的通用机制,用于将值从一组值转换为另一组值。这两个集合最终可能是不相交的,因此不仅仅是从一种枚举类型转换为另一种。
无论如何,以下代码按预期编译和运行,但我遇到了一些问题。的定义enumToString
,就在之前main()
,需要static_cast<const std::map<COLOR, std::string> &>
演员表。(FWIW,如果convert()
函数在映射中找不到键,则此构造函数会导致函数将键值作为字符串返回。)如何在没有这种强制转换的情况下编译代码,同时坚持使用 C++03?
可能是没有强制转换,编译器根本没有足够的类型信息来确定KeyToValue
要调用哪个构造函数。
#include <sstream>
#include <map>
#include "boost/assign.hpp"
template<typename K, typename V> // Forward reference.
class KeyToValue;
template<typename K, typename V> // K to V (value or callback default).
V convert(const KeyToValue<K, V> &t, const K &k)
{
typename std::map<K, V>::const_iterator it = t.m.find(k);
return it == t.m.end() ? (t.mc == NULL ? t.d : t.mc(k)) : it->second;
}
template<typename K> // K to string (auto default). (Use SFINAE for ostream&operator<<(K).)
std::string convert(const KeyToValue<K, std::string> &t, const K &k)
{
std::string v;
typename std::map<K, std::string>::const_iterator it = t.m.find(k);
if (it == t.m.end())
if (t.auto_default)
{
std::ostringstream oss;
oss << k;
v = oss.str();
}
else v = t.mc == NULL ? t.d : t.mc(k);
else v = it->second;
return v;
}
template<typename K, typename V> // Construct conversion object for convert().
class KeyToValue
{
public:
KeyToValue(const std::map<K, std::string> &m) : // To string w/auto default.
m(m), d(V()), mc(NULL), auto_default(true) { }
KeyToValue(const std::map<K, V> &m, const V &d) : // To V w/value default.
m(m), d(d), mc(NULL), auto_default(false) { }
KeyToValue(const std::map<K, V> &m, V (*mc)(const K &)) : // with callback.
m(m), d(V()), mc(mc), auto_default(false) { }
private:
const std::map<K, V> m;
const V d; // Default value.
V (*mc)(const K &); // Callback that returns default.
const bool auto_default; // Automatically create default from key?
template<typename K1, typename V1>
friend V1 convert(const KeyToValue<K1, V1> &t, const K1 &k);
template<typename K1>
friend std::string convert(const KeyToValue<K1, std::string> &t, const K1 &k);
};
#include <iostream>
enum COLOR { RED, BLUE, ORANGE, YELLOW, GOLD };
unsigned DefaultUnsigned(const COLOR &myEnum)
{
return -1;
}
const KeyToValue<COLOR, unsigned> enumToUnsigned(boost::assign::map_list_of
(ORANGE, 13) (YELLOW, 58), DefaultUnsigned );
const KeyToValue<COLOR, std::string> enumToString(
static_cast<const std::map<COLOR, std::string> &>(boost::assign::map_list_of
(ORANGE, "Orange") (YELLOW, "Yellow") ) );
int main()
{
std::cout << convert(enumToUnsigned, YELLOW) << std::endl;
std::cout << convert(enumToUnsigned, GOLD) << std::endl;
std::cout << convert(enumToString, YELLOW) << std::endl;
std::cout << convert(enumToString, GOLD) << std::endl;
}
这是使用演员表的正确控制台输出:
58
4294967295
Yellow
4
如果没有强制转换,g++ (-std=c++98) 会生成以下诊断信息:
prog.cc:64:43: error: call of overloaded 'KeyToValue(boost::assign_detail::generic_list<std::pair<COLOR, const char*> >&)' is ambiguous
(ORANGE, "Orange") (YELLOW, "Yellow") );
^
prog.cc:34:5: note: candidate: KeyToValue<K, V>::KeyToValue(const std::map<K, std::__cxx11::basic_string<char> >&) [with K = COLOR; V = std::__cxx11::basic_string<char>]
KeyToValue(const std::map<K, std::string> &m) : // To string w/auto default.
^
prog.cc:31:7: note: candidate: KeyToValue<COLOR, std::__cxx11::basic_string<char> >::KeyToValue(const KeyToValue<COLOR, std::__cxx11::basic_string<char> >&)
class KeyToValue
^
更新:这是简化版本。我怎样才能摆脱演员阵容?
#include <map>
#include "boost/assign.hpp"
struct KeyToValue {
KeyToValue(const std::map<int, bool> &m) { }
} intToBool(
static_cast<const std::map<int, bool> &>(
boost::assign::map_list_of (3, true)));
int main() { }