gpt4 book ai didi

c++ - 顺序独立的可变参数模板参数

转载 作者:行者123 更新时间:2023-12-03 07:02:03 26 4
gpt4 key购买 nike

我想创建一个“映射器”系统,即采用输入(模板)值并返回输出(模板)值的对象。映射器可以具有其他功能/特征。
样例代码:

// a trait
template <typename I, typename O>
class Writable
{
public:
virtual ~Writable() = default;
virtual O & operator()(I i) = 0;
};

// another trait
template <typename I, typename O>
class Bounded
{
public:
virtual ~Bounded() = default;
virtual O min() const = 0;
virtual O max() const = 0;
};

// base class
template <typename I, typename O, template<typename, typename> class ... Traits>
class IMap
: public Traits<I, O>...
{
public:
virtual ~IMap() = default;
virtual O operator()(I i) const = 0;
};

// a test mapper
class Test1
: public IMap<int, double, Writable, Bounded>
{
public:
double operator()(int i) const override { return _d; }
double & operator()(int i) override { return _d; }
double min() const override { return 0; }
double max() const override { return 1; }
private:
double _d;
};

// another test mapper
class Test2
: public IMap<int, double, Bounded, Writable>
{
public:
double operator()(int i) const override { return _d; }
double & operator()(int i) override { return _d; }
double min() const override { return 0; }
double max() const override { return 1; }
private:
double _d;
};

// a class to use mappers
template <typename I, typename O>
class MapUse
{
public:
// I would like this function to accept an IMap containing "at least" the Bounded trait
// Ideally this function signature should be like
void method_requiring_writable(IMap<I, O, Bounded> & map)
{
map(123)
map(123) = 0;
}
// I would like this function to accept an IMap containing "at least" the Bounded and Writable traits
// (no matter the actual order of those traits in variadic template parameter )
// Ideally this function signature should be like
void method_requiring_bounded_and_writable(IMap<I, O, Bounded, Writable> & map)
{
map(123);
map(123) = 0;
map.min();
}
};

void test()
{
Test1 t1;
Test2 t2;
Test2 * p = dynamic_cast<Test2*>(&t1); // << always return nullptr, of course !

MapUse<int, double> mu;
mu.method_requiring_writable(t1); // << does not compile, not the right type
mu.method_requiring_bounded_and_writable(t1); // << does not compile, not the right type

return;
}
行错误:mu.method_requiring_writable(t1):
error C2664: 'void MapUse<int,double>::method_requiring_writable(IMap<I,O,Bounded> &)': cannot convert argument 1 from 'Test1' to 'IMap<I,O,Bounded> &'
> with
> [
> I=int,
> O=double
> ]
行错误:mu.method_requiring_bounded_and_writable(t1):
error C2664: 'void MapUse<int,double>::method_requiring_bounded_and_writable(IMap<int,double,Bounded,Writable> &)': cannot convert argument 1 from 'Test1' to 'IMap<int,double,Bounded,Writable> &'
我完全理解为什么这行不通,但是我想让它起作用!
第一个问题:可变参数模板参数中的特征顺序不计数。可以通过强制开发人员遵守特征顺序来轻松解决此问题,但这并不理想
第二个问题:能够创建需要“至少”具有一组特定特征的映射的方法。对此一无所知。
第一种方法不是可变的,而是一个非常复杂的层次结构,其中包含许多虚拟遗产->丑陋得令人难以忍受,添加新特性真是痛苦
使用上面的代码,我考虑过在IMap中添加某种类型的强制转换运算符,但是没有这样做。
任何C++ / variadic /魔术专家可以帮助您吗?

最佳答案

您可以使用以下方式:

#include <type_traits>    

template <template <class, class> class... Traits,
std::enable_if_t<
std::is_base_of<Bounded<I, O>, IMap<I, O, Traits...>>::value
and std::is_base_of<Writable<I, O>, IMap<I, O, Traits...>>::value
, int> = 0>
void method_requiring_bounded_and_writable(IMap<I, O, Traits...> & map) {
// Do stuff
}

关于c++ - 顺序独立的可变参数模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64627009/

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