无法推断返回类型的模板参数
couldn't deduce template parameter of return type
我正在尝试使用std::function 创建一个抽象工厂
一个正常的工厂可以正常工作,但当我想返回一个工厂(另一个std::函数(而不是一个对象时,我会得到以下错误:
/home/nikolai/Projects/cpplearn/AbstractFactoryPattern/Main.cpp:9:45: error: no matching function for call to ‘factoryProducer(const char [6])’
auto shapeFactory = factoryProducer("shape")();
^
In file included from /home/nikolai/Projects/cpplearn/AbstractFactoryPattern/Main.cpp:3:
/home/nikolai/Projects/cpplearn/AbstractFactoryPattern/AbstractFactory.hpp:48:1: note: candidate: ‘template<class ReturnType> ReturnType abstractfactory::factoryProducer(const string&)’
factoryProducer(const std::string& tag = {})
^~~~~~~~~~~~~~~
/home/nikolai/Projects/cpplearn/AbstractFactoryPattern/AbstractFactory.hpp:48:1: note: template argument deduction/substitution failed:
/home/nikolai/Projects/cpplearn/AbstractFactoryPattern/Main.cpp:9:45: note: couldn't deduce template parameter ‘ReturnType’
auto shapeFactory = factoryProducer("shape")();
我对工厂的定义如下:
namespace factory
{
/**
* A template for a factory, which is just a std::function.
* See AnimalFactory.hpp for example usage.
*/
template <class ReturnType, class ...Args>
using Factory = std::function<ReturnType(Args...)>;
}
工厂生产商工作得很好,除了上一个,我正试图创建一个工厂工厂,退货类型必须是动态的:
namespace abstractfactory
{
using ShapeFactory = factory::Factory<std::unique_ptr<Shape>>;
using ColorFactory = factory::Factory<std::unique_ptr<Color>>;
ShapeFactory shapeFactoryProducer(const std::string& tag = {})
{
return [=]
{
if(tag == "rectangle")
return std::unique_ptr<Shape>(new Rectangle());
else if(tag == "circle")
return std::unique_ptr<Shape>(new Circle());
else if(tag == "square")
return std::unique_ptr<Shape>(new Square());
else
return std::unique_ptr<Shape>(nullptr);
};
}
ColorFactory colorFactoryProducer(const std::string& tag = {})
{
return [=]
{
if(tag == "green")
return std::unique_ptr<Color>(new Green());
else if(tag == "blue")
return std::unique_ptr<Color>(new Blue());
else if(tag == "red")
return std::unique_ptr<Color>(new Red());
else
return std::unique_ptr<Color>(nullptr);
};
}
template <class ReturnType>
ReturnType
factoryProducer(const std::string& tag = {})
{
return [=]
{
if(tag == "shape")
return shapeFactoryProducer;
else if(tag == "color")
return colorFactoryProducer;
else
return [=] {};
};
}
}
如何实现函数factoryProducer
的动态返回类型?
编辑:新代码(仍然不起作用(:
struct FactoryType {};
struct ShapeType {};
struct ColorType {};
template <class TFactory, class ReturnType>
ReturnType factoryProducer(TFactory tag)
{
return [=]
{
if constexpr (std::is_same<TFactory, ShapeType>::value)
return shapeFactoryProducer;
else if constexpr (std::is_same<TFactory, ColorType>::value)
return colorFactoryProducer;
};
}
auto shapeFactoryProducer = factoryProducer(ShapeType());
您的返回类型演绎语法太离谱了。这是正确的一个(需要C++17(。
template <class TFactory>
auto factoryProducer(TFactory tag)
{
if constexpr (std::is_same<TFactory, ShapeType>::value)
return shapeFactoryProducer;
else if constexpr (std::is_same<TFactory, ColorType>::value)
return colorFactoryProducer;
}
相关文章:
- 函数作为模板参数,是否对返回类型强制约束
- 在 c++ 中将函数返回类型指定为模板参数
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- 基于类型作为参数的字符串表示形式具有不同返回类型的函数
- 具有"templated"返回类型和参数的函数指针
- 如何创建两个具有相同名称和不同返回类型并基于布尔参数运行的函数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 有没有办法根据 lambda 参数返回类型部分专用化我的模板化函数?
- 如何定义依赖于参数包转换的函数的返回类型
- 无法推断返回类型的模板参数
- C++模板;作为模板参数传递的函数的自动推导返回类型
- 如何在模板参数中分离函数类型返回类型和参数
- 如何修复用于根据参数返回不同类型的模板类中的错误C2679?
- 为什么函数返回类型中不允许参数推导?
- 模板化函数以从输入参数推断返回类型 stl-container
- 将 2D 数组作为函数参数,并返回 2D 数组作为函数的返回类型
- 推论成员函数参数 /返回类型
- C++ 基于模板参数返回类型的模板函数
- 根据参数返回类型
- 隐藏规则-参数/返回类型必须相同吗?