gpt4 book ai didi

c++ - 带有 g++ -O2(或 -Os、-O、-O1、...)的模板警告

转载 作者:行者123 更新时间:2023-11-30 02:03:34 24 4
gpt4 key购买 nike

我收到一些关于模板和 g++ -Os 的警告。为什么 ?如何使用 -Os -Winline 删除这些警告?

编辑 1:g++ v4.6.1 如果我将 uint32_t 替换为 unsigned int 我的主要错误已更正:(此错误已报告: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())在调用“Player::”时内联失败: ~Player()':不太可能调用,代码大小会增加 [-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

main.cpp

#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]

最佳答案

你的动机有点不清楚......

您希望警告消失吗?删除 -Winline 选项。这就是它的作用:它对非内联内联函数发出警告。

您是否希望内联这些函数,即使代码可能变得更大/更慢?您将不得不尝试内联的启发式方法。您可以尝试多种选择:

  • 在相关函数中添加__attribute__(always_inline)
  • 用一个大的N编译-finline-limit=N
  • 使用编译器选项 --param NAME=VALUE。控制内联的参数有几十个(max-inline-insns-single, max-inline-insns-auto, large-function-insnsinline-unit-growth 等)您可以阅读 info gcc 以获取详细信息。

关于c++ - 带有 g++ -O2(或 -Os、-O、-O1、...)的模板警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11724235/

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