我收到一些关于模板和 g++ -Os 的警告。为什么 ?如何使用 -Os -Winline 删除这些警告?
编辑 1:g++ v4.6.1 如果我用 unsigned int 替换uint32_t我的主要错误得到更正:(此错误已报告:http ://gcc.gnu.org/bugzilla/show_bug.cgi?id=52888 )
Event.h:109:12: attention : inlining failed in call to ‘uint32_t Event<ParamT>::attach(ListenerT*, bool (ListenerT::*)(ParamT)) [with ListenerT = Listener, ParamT = unsigned int, uint32_t = unsigned int]’: mismatched arguments [-Winline]
编辑 2:以下警告似乎是 g++ 错误(http://gcc.gnu.org/ml/gcc-help/2012-07/msg00029.html)(我没有任何 ~Player())inlining failed in call to ‘Player::~Player()’: call is unlikely and code size would grow [-Winline]
编辑 3:要删除以前的警告,请添加Player::~Player() __attribute__ ((noinline)) {}
.
测试代码:Event.h
#ifndef EVENT_H
#define EVENT_H
#include <map>
#include <stdint.h>
/***************************************************************************//*!
* @brief Collect Listener
* @tparam ParamT Param type of the function
*/
template<typename ParamT>
class EventHandlerBase
{
public:
virtual bool notify( ParamT param ) = 0;
};
/***************************************************************************//*!
* @brief Conteneur d'un receveur d'event.
* @tparam ListenerT Object type
* @tparam ParamT Param type of the function
*/
template<typename ListenerT, typename ParamT>
class EventHandler : public EventHandlerBase<ParamT>
{
private:
typedef bool (ListenerT::*PtrMember)(ParamT);
private:
ListenerT* m_object;//!< Object listener
PtrMember m_member;//!< Function listener
public:
/********************************************************************//*!
* @brief Constructor
* @param[in] object Instance listener
* @param[in] member Function listener ( Function need to be a public member of {object} )
* @return[NONE]
*/
EventHandler( ListenerT* object, PtrMember member )
{
m_object = object;
m_member = member;
}
/*******************************************************************//*!
* @brief Emit a signal to listeners
* @param[in] param Data
* @return FALSE for breaking event loop
*/
bool notify( ParamT param )
{
return (m_object->*m_member)(param);
}
};
/***************************************************************************//*!
* @brief Event system
* @tparam ParamT Param type of the function
*/
template<typename ParamT>
class Event
{
private:
typedef typename std::map< uint32_t, EventHandlerBase<ParamT>* > HandlersMap;
private:
HandlersMap m_handlers;//!< Contient la liste des instances::fonctions en écoute sur cet event
uint32_t m_counter;//!< Permet de gérer les id
public:
/********************************************************************//*!
* @brief Constructor
* @return[NONE]
*/
Event()
{
m_counter = 0;
}
/********************************************************************//*!
* @brief Destructor
* @return[NONE]
*/
~Event()
{
typename HandlersMap::iterator it = m_handlers.begin();
for(; it != m_handlers.end(); it++)
{
if( it->second )
delete it->second;
}
}
/*******************************************************************//*!
* @brief Link a function and instance to this event
* @param[in] object Instance listener
* @param[in] PtrMember Function listener ( Function need to be a public member of {object} )
* @return Connection ID
*
* @warning DO NOT FORGET TO CALL Event::detach(uint32_t) if you delete {object}
*/
template<typename ListenerT>
uint32_t attach( ListenerT* object, bool (ListenerT::*PtrMember)(ParamT) )
{
m_handlers[m_counter] = new EventHandler<ListenerT,ParamT>(object, PtrMember);
m_counter++;
return m_counter-1;
}
/*******************************************************************//*!
* @brief Emit a signal to listeners
* @param[in] param Data
* @return[NONE]
*/
void notify( ParamT param )
{
typename HandlersMap::iterator it = m_handlers.begin();
for(; it != m_handlers.end(); it++)
{
if( !it->second->notify(param) )
return ;
}
}
/*******************************************************************//*!
* @brief Unlink a listener.
* @param[in] id Connection ID from Event::attach
* @return TRUE if removed
*/
bool detach( uint32_t id )
{
typename HandlersMap::iterator it = m_handlers.find(id);
if( it == m_handlers.end() )
return false;
delete it->second;
m_handlers.erase(it);
return true;
}
};
#endif // EVENT_H
主文件
#include <stdio.h>
#include "Event.h"
class Player
{
public:
Event<uint32_t> e_speed;
private:
uint32_t m_speed;
public:
Player()
{
m_speed = 15;
}
void setSpeed( uint32_t speed )
{
m_speed = speed;
e_speed.notify(speed);
}
};
class Listener
{
private:
Player m_player;
public:
Listener()
{
m_player.e_speed.attach(this, &Listener::SLOT_speed);
}
bool SLOT_speed( uint32_t speed )
{
printf("Speed changed to %u\n", speed);
return true;
}
Player* player()
{
return &m_player;
}
};
int main()
{
Listener l;
l.player()->setSpeed(42);
return 0;
}
使用以下命令编译此代码:g++ -Os -W -Wall -Winline *.cpp 您将收到以下警告:
Event.h:109:12: attention : inlining failed in call to ‘uint32_t Event<ParamT>::attach(ListenerT*, bool (ListenerT::*)(ParamT)) [with ListenerT = Listener, ParamT = unsigned int, uint32_t = unsigned int]’: mismatched arguments [-Winline]
main.cpp:32:56: attention : appelé d'ici [-Winline]
main.cpp:4:7: attention : inlining failed in call to ‘Player::~Player()’: call is unlikely and code size would grow [-Winline]
main.cpp:31:3: attention : appelé d'ici [-Winline]
main.cpp:4:7: attention : inlining failed in call to ‘Player::~Player()’: call is unlikely and code size would grow [-Winline]
main.cpp:24:7: attention : appelé d'ici [-Winline]
main.cpp:4:7: attention : inlining failed in call to ‘Player::~Player()’: call is unlikely and code size would grow [-Winline]
main.cpp:24:7: attention : appelé d'ici [-Winline]