c++ - 模板类和别名导致编译错误

标签 c++ templates alias

我一直在使用 wxWidgets 开发一个应用程序(细节,但不是特定于问题)。我有一个名为 MyPanel 的类,其中有一个计时器,可以根据需要启动和停止。我将计时器用于许多不同的目的,尽管一次只能激活其中一个目的。为此,我有两种方法: StartTimer 和 StopTimer,两者都必须传递给定时器处理程序,以便在定时器启动或停止时绑定(bind)/解除绑定(bind)。为了简化代码,我为处理程序类型使用了一个别名。这是声明:

using TimerHandler = void (MyPanel::*) (wxTimerEvent&);

class MyBasePanel : public wxGLCanvas
{
   ...
    std::unique_ptr<ShaderProgram> m_program;
    ...
}

class MyPanel : public MyBasePanel
{
    ...
    void StartTimer(TimerHandler handler);
    ...
}

因此,一切都可以编译和运行。现在的问题。我想要从 MyBasePanel 派生多个类,每个类使用不同的 ShaderProgram 类和不同的处理程序。所以,我改变了我的类声明如下:

template <typename T>
class MyBasePanel : public wxGLCanvas
{
    ...
    std::unique_ptr<T> m_program;
    ...
}

template <typename T>
class MyPanel : public MyBasePanel<T>
{
    ...
}

现在,当我尝试编译时,Visual Studio 在别名上给出了这个错误:

'MyPanel': use of class template requires template argument list

好的,所以我将别名更改为:

template <typename T>
using TimerHandler = void (MyPanel<T>::*) (wxTimerEvent&)

和 StartTimer(和 StopTimer)方法声明:

void StartTimer(TimerHandler<T> handler);

现在 Visual Studio 输出:

'MyPanel::StartTimer': unable to match function definition to an existing declaration

note: see declaration of 'MyPanel::StartTimer'

note: definition

'void MyPanel:StartTimer(void (__cdecl MyPanel::* )(wxTimerEvent &))'

existing declarations

'void MyPanel:StartTimer(void (__cdecl MyPanel::* )(wxTimerEvent &))'

注意定义和声明是一样的。一些调查表明 C++ 标准实际上不允许以这种方式组合别名和模板。

我已经针对这个问题提出了一个我认为可行的潜在解决方案。不是将处理程序传递给 StartTimer 和 StopTimer 方法,而是传递一个标志,然后测试该标志并绑定(bind)/取消绑定(bind)匹配的处理程序。我没有尝试过这个,但我认为它会起作用。我担心的是这看起来很笨拙而且很像 C;应该有一个更像 C++ 的解决方案。

如何将上面的内容更改为“工作”?

最佳答案

我将继续发布此解决方案,因为它适用于 clang 和 g++。看来您使用的是 visual-studio,因此,我无法判断此解决方案是否适合您。

#include <iostream>

template <typename U>
using Timer = void (U::*)(); // This is your timer.

// A small template meta-program to make sure that your
// Timer<T> type actually points to some callable object.
// It is needed to make sure that the type T in 'Timer<T>'
// is indeed Mypanel or similar class and that has a function named
// cb (can be changed as well)
namespace meta_detail {
template <typename T>
constexpr auto has_cb(const T& obj, int) -> decltype(std::declval<T&>().cb(), bool())
{
  return true;
}

template <typename T>
constexpr auto has_cb(const T& obj, long) -> decltype(std::declval<T&>().cb(), bool())
{
  return false;
}
}

template <typename T>
class Test {
public:
  template <typename U>
  void take_it(Timer<U> t) {
    // This is where the meta-program helps you. Compile time
    // Checking.
    static_assert(meta_detail::has_cb(U(), 0), "Nopes!");
    (this->*t)();
  }

  void cb() {
    std::cout << "Callback" << std::endl;
  }
};

int main() {
  Test<int> tt;
  Timer<Test<int>> timer = &Test<int>::cb;
  tt.take_it(timer);
  return 0;
}

关于c++ - 模板类和别名导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37509611/

相关文章:

c++ - 临时 std::strings 返回垃圾

c++ - 如何通过重载运算符 << 来访问 protected 函数? C++

c++ - 模板参数推导 - 推导 T 并使用 T&&

c++ - 模板函数中的引用类型转换?

c++ - 转发引用可以使用别名模板作为别名吗?

c++ - C++ 中的基本 QT - 难以理解

c++ - g++: 警告:整数常量对于 ‘long’ 类型来说太大

c++链表模板和节点

mysql - SQL 查询选择和更新

linux - 将 "man"替换为 "?"