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

时间:2012-07-30 15:08:44

标签: c++ events templates optimization warnings

我得到一些模板和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)(我没有〜播放器())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

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]

2 个答案:

答案 0 :(得分:4)

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

你想要警告消失吗?删除-Winline选项。这就是它的作用:它警告非内联内联函数。

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

  • __attribute__(always_inline)添加到相关功能。
  • 使用大-finline-limit=NN进行编译。
  • 使用编译器选项--param NAME=VALUE。有几十个参数可以控制内联(max-inline-insns-singlemax-inline-insns-autolarge-function-insnsinline-unit-growth等。)您可以阅读info gcc来获取详细信息。< / LI>

答案 1 :(得分:0)

似乎g ++在将它们用作模板参数之前扩展了typedef,但是在将它们与前者进行比较时,无法扩展未用作模板参数的typedef。

更简单的代码版本说明了这一点:

#include <iostream>

typedef unsigned int uint32_t;

template <typename T>
class Foo {
    public :
        template <typename C>
        void fun(C* c, void (C::*f)(T)) {
            (c->*f)(42);
        }
};

class BarBad {
    private :
        Foo<uint32_t> foo;

    public :
        BarBad() {
            foo.fun(this, &BarBad::fun);
        }
        void fun(uint32_t t) {
            std::cout << t << std::endl;
        }
};

template <typename T>
class BarGood {
    private :
        Foo<T> foo;

    public :
        BarGood() {
            foo.fun(this, &BarGood<T>::fun);
        }
        void fun(T t) {
            std::cout << t << std::endl;
        }
};

int main(void) {
    BarBad bb;
    BarGood<uint32_t> bg;
    return 0;
}

此代码仅针对BarBad代码发出警告:

test.cpp: In function ‘int main()’:
test.cpp:9: warning: inlining failed in call to ‘void Foo<T>::fun(C*, void (C::*)(T)) [with C = BarBad, T = unsigned int]’: function not considered for inlining
test.cpp:20: warning: called from here

BarBad中,当调用Foo<uint32_t>::fun时,第二个参数的类型为void (BarBad::*)(uint32_t),但被调用的函数需要void (BarBad::*)(unsigned int),并且无法看到{{1} }和uint32_t是相同的类型。

unsigned int中,当调用BarGood<uint32_t>时,第二个参数的类型为Foo<uint32_t>::fun,它与被调用函数所期望的匹配。

这对我来说感觉像编译器错误。有点搜索出现了这个错误报告:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52888

我还没有测试过那里的解决方案是否也解决了这个问题,但听起来确实如此。