gpt4 book ai didi

c++ - 在模板和非模板代码之间进行交互时替换 switch 语句

转载 作者:行者123 更新时间:2023-11-30 04:05:53 26 4
gpt4 key购买 nike

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;
}
*/
}

我在实现过程中遇到的问题:

  1. 清晰易读
  2. 模板参数必须是类型,必须将值包装在类型中 ( template <typename T, T VAL> struct value_container { static constexpr T value() {return VAL;} }; )
  3. 目前没有检查/强制选择器都是类型-值对。

唯一的优点:

  1. 删除代码重复。
  2. 如果 case 语句变高/do_things 的内容变高,那么我们可以缩短一点。

有没有人做过类似的事情或者有更好的方法?

最佳答案

您始终可以遍历由 type_param 索引的类型列表,如:

struct foo 
{
virtual ~foo() = default;
/* ... */
};

template<typename T>
struct bar : foo
{ /* ... */ };


template<typename TL>
struct foo_maker;

template<template<typename...> class TL, typename T, typename... Ts>
struct foo_maker<TL<T, Ts...>>
{
template<typename... Us>
std::unique_ptr<foo> operator()(int i, Us&&... us) const
{
return i == 1 ?
std::unique_ptr<foo>(new bar<T>(std::forward<Us>(us)...)) :
foo_maker<TL<Ts...>>()(i - 1, std::forward<Us>(us)...); }
};

template<template<typename...> class TL>
struct foo_maker<TL<>>
{
template<typename... Us>
std::unique_ptr<foo> operator()(int, Us&&...) const
{ return nullptr; }
};


template<typename...>
struct types;


template<typename... Us>
std::unique_ptr<foo> make_foo(int typed_param, Us&& us...)
{ return foo_maker<types<int, float, double, uint8_t>>()(typed_param, std::forward<Us>(us)...); };

注意:这个工厂函数是 O(n)(尽管聪明的编译器可以让它成为 O(1)),而 switch 语句版本是 O(1)。

关于c++ - 在模板和非模板代码之间进行交互时替换 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23092121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com