Is typedef inside of a function body a bad programming practice?

前端 未结 2 1619
不思量自难忘°
不思量自难忘° 2021-02-06 22:12

I have some class C and want to pass address of its instance and method to some functor in a test function Test_C_Foo1(). Functor is a template class

相关标签:
2条回答
  • 2021-02-06 22:57

    It's good. It's legal and localized.

    0 讨论(0)
  • 2021-02-06 23:11

    IMHO, if the typedef is just to avoid typing or to make the pointer-to-member-function less cumbersome, as your example shows, thenit is fine.

    But if the typedef reveals some particular concept, then it should be visible outside of the function.

    A quick test to check it is the name of the typedef:

    typedef int(C::*MEMFN1)(const std::string&); //OK: as local typedef, just an abbreviation
    
    typedef int(C::*ACTION)(const std::string&); //Not so OK: Action is a new concept
    
    0 讨论(0)
提交回复
热议问题