gpt4 book ai didi

c++ - 如何为参数为泛型类型的模板类中的方法定义函数签名

转载 作者:行者123 更新时间:2023-11-28 05:45:28 26 4
gpt4 key购买 nike

我写了一个模板类

template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void(T&)> fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};

template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
...
}

template<typename T, typename U>
class AMultiNotifier : public ANotifier<T, U>
{
...
}

基类在 lambda 中提供函数签名。每次实现函数或想要创建特定函数签名的堆栈变量时,我都必须重复定义函数签名。

我想知道是否有办法创建一个 typedef

像这样的东西

template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
typedef std::function<void(T&)> NotificationFn
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, NotificationFn fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};

template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
...
virtual std::weak_ptr<ACancelableToke> RegisterNotification( U msgType, NotificationFn fn );
}

template<typename T, typename U>
class AMultiNotifier : public ANotifier<T, U>
{
...
virtual std::weak_ptr<ACancelableToke> RegisterNotification( U msgType, NotificationFn fn );
}

我是否需要在每个模板子类中声明一个 typedef。此外,当实际为特定类型的函数创建堆栈变量时,我将如何声明 fn.例如。 NotificationFn<uint32_t> fn;

我使用了上面的内容,但出现了这个错误:

/Users/kartik/Projects/pancam/hardware/neolib/inc/ANotifier.h:36:76: error: unknown type name 'NotificationFnOld' virtual std::weak_ptr RegisterNotification( U msgType, NotificationFnOld fn )

哪里NotificationFnOldNotificationFn相同

我想做的是能够在一个地方更改函数签名(即添加/删除参数)并将其绑定(bind)到 typedef。我不介意在每个子类中声明新的 typedef,只要我可以在一个地方更改签名(当然我将不得不更改函数实现,但我想避免修改中间变量和东西)。

编辑

我将 typedef 修改为 using仍然有类似的错误。这是我用的...而不是 typedef std::function<void(T&)> NotificationFn;我用了using NotificationFnOld = std::function<void( T& )>;

这是我得到的错误。

/Users/kartik/Projects/pancam/hardware/neolib/inc/ANotifier.h:36:76: error: unknown type name 'NotificationFnOld' virtual std::weak_ptr RegisterNotification( U msgType, NotificationFnOld fn )

子类报错ASingleNotifier在覆盖函数的原型(prototype)中 RegisterNotification .当在 ANotifier 中声明的原型(prototype)中使用别名时,它不会提示.我如何在我的子类中干净地使用别名?

注:NotificationFnOldNotificationFn在这个问题中可以互换使用,我实际上只使用 NotificationFnOld在我的代码中。

编辑

如果需要详细信息,这里是我正在尝试的代码。应该与ACancelable没有关系但如果需要更多信息,我会提供。

template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
using NotificationFn = std::function<void( T&, std::weak_ptr<ACancelableToken> )>;
using NotificationFnOld = std::function<void( T& )>;
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};

template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
public:
virtual ~ASingleNotifier()
{ }

virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn )
{
std::weak_ptr<ACancelableToken> retval;
std::unique_lock <std::mutex> lk( m_mtx );
std::shared_ptr <ATypedCancelableToken<U>> tmp( std::make_shared<ATypedCancelableToken<U>>( *this, msgType ));
if( m_notifierMap.find( msgType ) == m_notifierMap.end() ) {
m_notifierMap[ msgType ] = std::make_pair( fn, tmp );
retval = tmp;
}
return retval;
}

virtual void CancelWith( std::shared_ptr<ACancelableToken> spBaseToken ) const
{
try {
auto spToken = std::dynamic_pointer_cast<ATypedCancelableToken<U>>( spBaseToken );
std::unique_lock<std::mutex> lk( this->m_mtx );
m_notifierMap.erase( spToken->m_msgType );
}
catch ( std::bad_cast exp ) { }
}

virtual void Notify( U msgType, T& value )
{
m_mtx.lock();
auto it = m_notifierMap.find( msgType );
if ( it != m_notifierMap.end() && it->second.first ) {
auto fn = it->second.first;
m_mtx.unlock();
fn( value );
}else {
m_mtx.unlock();
}
}

protected:
mutable std::map <U, std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>, std::shared_ptr<ATypedCancelableToken<U>>>> m_notifierMap;
mutable std::mutex m_mtx;
};

template<typename T, typename U>
class AMultiNotifier : public ANotifier<T,U>
{
protected:
class AmnCancellableToken : public ATypedCancelableToken<U>
{
public:
AmnCancellableToken( const ACancelable& cancellable,
U msgType,
typename std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>::iterator it ) :
ATypedCancelableToken<U>{ cancellable, msgType }, m_it{ it } { }
~AmnCancellableToken() {}
const typename std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>::iterator m_it;
};

public:
~AMultiNotifier() { }
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, std::function<void( T&, std::shared_ptr<ACancelableToken> )> fn )
{
std::weak_ptr<ACancelableToken> retval;
std::unique_lock <std::mutex> lk( m_mtx );
std::shared_ptr<AmnCancellableToken> token;

m_notifierMap[ msgType ].push_back( std::make_pair( fn, token) );
auto it = m_notifierMap[msgType].end();
token = std::make_shared<AmnCancellableToken>( *this, msgType, --it );

m_notifierMap[ msgType ].back().second = token;
retval = token;
return retval;
}

virtual void CancelWith( std::shared_ptr<ACancelableToken> spBaseToken ) const
{
try {
auto spToken = std::dynamic_pointer_cast<AmnCancellableToken>( spBaseToken );
std::unique_lock<std::mutex> lk( this->m_mtx );
if ( !m_notifierMap[ spToken->m_msgType ].empty()) { //If the list of handler is not empty
m_notifierMap[ spToken->m_msgType ].erase( spToken->m_it ); //Delete the handler in list
if ( m_notifierMap[ spToken->m_msgType ].empty()) //If the list is now empty
m_notifierMap.erase( spToken->m_msgType ); // Delete the msgType Key element
}
} catch ( std::bad_cast exp ) { }
}

virtual void Notify( U msgType, T& value )
{
m_mtx.lock();
auto anotherCopy = m_notifierMap;
m_mtx.unlock();
typename std::map<U, std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>, std::shared_ptr<AmnCancellableToken>>>>::iterator ait =
anotherCopy.find( msgType );

if( ait != anotherCopy.end() &&
!ait->second.empty() ) {
for( auto ait2 = ait->second.begin(); ait2 != ait->second.end(); ait2++ )
if( ait2->first )
ait2->first( value );
}
}

protected:
mutable std::map <U, std::list<std::pair<std::function<void( T&, std::shared_ptr<ACancelableToken> )>,std::shared_ptr<AmnCancellableToken>>>> m_notifierMap;
mutable std::mutex m_mtx;
};

感谢您的帮助/建议

卡提克

最佳答案

您需要限定 NotificationFn:

  virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, typename ANotifier<T,U>::NotificationFn fn );

或者如果你要多次使用它,添加一个 using 语句:

using NotificationFn = typename ANotifier<T,U>::NotificationFn;

在每个派生类中。

这是一个完整的例子:

#include <iostream>
#include <functional>
#include <memory>

struct ACancelable {};
struct ACancelableToken {};

template<typename T, typename U>
class ANotifier : public ACancelable
{
public:
using NotificationFn = std::function<void(T&)>;
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, NotificationFn fn ) = 0;
virtual void Notify( U msgType, T& value ) = 0;
};

template<typename T, typename U>
class ASingleNotifier : public ANotifier<T, U>
{
virtual std::weak_ptr<ACancelableToken> RegisterNotification( U msgType, typename ANotifier<T,U>::NotificationFn fn );
};

int main() {
// your code goes here
return 0;
}

http://ideone.com/sXVkvw

关于c++ - 如何为参数为泛型类型的模板类中的方法定义函数签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36317723/

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