gpt4 book ai didi

c++ - 指向模板类方法的指针的 Typedef

转载 作者:行者123 更新时间:2023-11-30 02:55:58 25 4
gpt4 key购买 nike

标题总结了我的问题 - 我需要一个通用的 typedef 作为指向模板类方法的指针,如下面的代码所述。 typedef 需要是通用的。

template<typename TYPE>
struct MyClass {
const TYPE& get() const {}
};

// this is okay:
typedef void (MyClass<int>::*ParticleMethodPtr)( int );

// now I just need to typedef this so I can
// use the pointer on methods other than int

// using typedef, not okay:
template< TYPE >
typedef void (MyClass<TYPE>::*ParticleMethodPtr)( TYPE );

最佳答案

在 C++11 中:

template<typename TYPE>
using ParticleMethodPtr = const TYPE&(MyClass<TYPE>::*)() const;

ParticleMethodPtr<int> p = &MyClass<int>::get;

关于c++ - 指向模板类方法的指针的 Typedef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007153/

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