【发布时间】:2014-04-15 18:57:32
【问题描述】:
X:
我看到的一个常见模式是函数的底层代码是模板,但由于“原因”,模板代码在上层不可用(从厌恶到接口中的模板,需要共享库而不是向客户公开实现,在运行时而不是编译时读取类型设置等)。
这通常会导致以下情况:
struct foo { virtual void foo() = 0;}
template <typename T> struct bar : public foo
{
bar( /* Could be lots here */);
virtual void foo() { /* Something complicated, but type specific */}
};
然后是初始化调用:
foo* make_foo(int typed_param, /* More parameters */)
{
switch(typed_param)
{
case 1: return new bar<int>(/* More parameters */);
case 2: return new bar<float>(/* More parameters */);
case 3: return new bar<double>(/* More parameters */);
case 4: return new bar<uint8_t>(/* More parameters */);
default: return NULL;
}
}
这是令人讨厌、重复且容易出错的代码。
所以我对自己说,自己说我,必须有更好的方法。
Y:
这是我做的。大家有更好的办法吗?
////////////////////////////////////
//////Code to reuse all over the place
///
template <typename T, T VAL>
struct value_container
{
static constexpr T value() {return VAL;}
};
template <typename J, J VAL, typename... Ts>
struct type_value_pair
{
static constexpr J value() {return VAL;}
template <class FOO>
static auto do_things(const FOO& foo)->decltype(foo.template do_things<Ts...>()) const
{
foo.template do_things<Ts...>();
}
};
template <typename T>
struct error_select
{
T operator()() const { throw std::out_of_range("no match");}
};
template <typename T>
struct default_select
{
T operator()() const { return T();}
};
template <typename S, typename... selectors>
struct type_selector
{
template <typename K, class FOO, typename NOMATCH, typename J=decltype(S::do_things(FOO()))>
static constexpr J select(const K& val, const FOO& foo=FOO(), const NOMATCH& op=NOMATCH())
{
return S::value()==val ? S::do_things(foo) : type_selector<selectors...>::template select<K, FOO, NOMATCH, J>(val, foo, op);
}
};
template <typename S>
struct type_selector<S>
{
template <typename K, class FOO, typename NOMATCH, typename J>
static constexpr J select(const K& val, const FOO& foo=FOO(), const NOMATCH& op=NOMATCH())
{
return S::value()==val ? S::do_things(foo) : op();
}
};
////////////////////////////////////
////// Specific implementation code
class base{public: virtual void foo() = 0;};
template <typename x>
struct derived : public base
{
virtual void foo() {std::cout << "Ima " << typeid(x).name() << std::endl;}
};
struct my_op
{
template<typename T>
base* do_things() const
{
base* ret = new derived<T>();
ret->foo();
return ret;
}
};
int main(int argc, char** argv)
{
while (true)
{
std::cout << "Press a,b, or c" << std::endl;
char key;
std::cin >> key;
base* value = type_selector<
type_value_pair<char, 'a', int>,
type_value_pair<char, 'b', long int>,
type_value_pair<char, 'c', double> >::select(key, my_op(), default_select<base*>());
std::cout << (void*)value << std::endl;
}
/* I am putting this in here for reference. It does the same
thing, but the old way: */
/*
switch(key)
{
case 'a':
{
base* ret = new derived<int>();
ret->foo();
value = ret;
break;
}
case 'b':
{
base* ret = new derived<char>();
ret->foo();
value = ret;
break;
}
case 'c':
{
base* ret = new derived<double>();
ret->foo();
value = ret;
break;
}
default:
return NULL;
}
*/
}
我在实现中看到的问题:
- 像泥一样清晰可读
- 模板参数必须是类型,必须将值包装在类型中 (
template <typename T, T VAL> struct value_container { static constexpr T value() {return VAL;} };) - 目前没有检查/强制选择器都是类型-值对。
唯一的优点:
- 删除重复代码。
- 如果 case 语句变高/do_things 的内容变高,那么我们可以缩短一点。
有没有人做过类似的事情或有更好的方法?
【问题讨论】:
-
所以,我看到你的建议最大的问题(好吧,可读性可能更糟......)是它只适用于
typed_param的常量值,因为模板不适用于“运行时变量”。 (编辑:除非泥很不清楚,否则我不明白它是如何工作的!) -
@MatsPetersson 我同意如果可以有非常量 typed_params 会很好,但是 switch 语句还需要“常量” typed_params 用于它的 case 语句......在 C++ 中是唯一的方法那会不一样。 “动态”部分只是输入参数以及“foo”和“NOMATCH”的任何配置。编辑:它也完全有效,尽管它很丑,所以如果有任何关于它的具体问题,请告诉我!
-
我有点不清楚你想在这里完成什么?您原始示例中的 make_foo 函数很容易替换为模板函数,该函数消除了冗余样板,但您说客户端代码不能暴露给模板?然后你的新代码做了一堆其他的事情,最终要求客户端代码使用模板......你能更清楚你想要做什么吗?
-
如果您只想替换
switch语句,那么您可以使用std::map<case_type, factoryFunction>其中case_type是您打开的任何类型,factoryFunction是@987654330 @ 返回foo*类型的值。 -
@YoungJohn hrm...工厂函数将指向模板函数 bar
,以避免代码重复...这是有道理的。如果有模板化的 lambdas 会更有意义。