我目前正在开发一个GUI应用程序。我的GUI应用程序类是一个单例,它依赖于一个模板化的Mutex和一个模板化的事件处理任务。
//GUIApplication.hpp
#include <cassert>
template<typename Task_T, typename Mutex_T>
class GUIApplication
{
public:
GUIApplication()
{
m_pInstance = this;
}
static Mutex_T& getMutexInstance()
{
assert(nullptr != m_pInstance);
return m_pInstance->m_GuiMutex;
}
private:
inline static GUIApplication<Task_T, Mutex_T>* m_pInstance = nullptr;
Task_T m_eventTask;
Mutex_T m_GuiMutex;
};
字符串
现在我有一些其他的类需要在做事情之前锁定互斥锁。但是我不想用这些其他的类制作模板。有没有一种方法可以从GUIApplication的当前示例中“派生”或“保存”模板类型。
//Foo.hpp
class Foo
{
public:
Foo();
};
//Foo.cpp
#include <mutex>
Foo::Foo()
{
//here (obviously) the template auto argument deduction fails
auto lock = std::scoped_lock(GUIApplication::getMutexInstance());
//do stuff here....
}
型
Thx guys for your help:)
2条答案
按热度按时间4uqofj5v1#
显然,您无法避免为
GUIApplication
指定模板参数,因此GUIApplication::getMutexInstance
无法工作。(1)从别名构建
GUIApplication
但是,巧妙地使用别名可以帮助您避免重复:
字符串
(2)提取
GUIApplication
的别名类似地,您可以向
GUIApplication
添加别名成员,以便更轻松地从中提取模板参数:型
您经常在标准库中看到这种模式,例如
std::string::size_type
,std::vector::value_type
等。liwlm1x92#
你可以实现一个自定义的类型trait来通过索引获取模板参数。
我已经做了一个依赖于
std::tuple
和std::tuple_element_t
的快速实现,但是你也可以在没有这些的情况下正确地实现它(如果需要的话)。字符串
然后在代码中使用它如下:
型
这并不能修复编译失败的示例,它只是一种获取模板参数的替代方法。
为了真正修复你的错误,我更喜欢
GuiApplication
的别名,就像Jan Schultke建议的那样,让编译器推导出所有的东西。