4

I have a huge project that I am re-factoring, there are a lot of define statements that I am changing to enums, defines that I am also changing to const variables in a common file.

while I was re-factoring I found that some of the defines were repeated in sub classes header. some of the defines, and structs were not even used, or used to.

how can I make the compiler point them out, like when the compiler show unused variables?

I want to get ride of them,

right now I comment them out, and find what is needed manually! is there other ways

pnuts
  • 58,317
  • 11
  • 87
  • 139
aah134
  • 860
  • 12
  • 25

1 Answers1

1

I hate #define contant in one big .h file too. so ,I find a way to define contant by C++ type system. this was my work two year's ago.

------------------------------------------------------
id_system.h
------------------------------------------------------
#pragma once
template<int N>
struct ID_FACTORY{
    enum {_ID=N};
    static const unsigned int m_duplicate_checker; 
};

#define ID_DECLARE(classname, number) \
struct classname{ \
    typedef ID_FACTORY<number> MYID_TYPE; \
    static const unsigned int ID; \
}; \
------------------------------------------------------
a.h 
------------------------------------------------------
#pragma once
#include "id_system.h"
ID_DECLARE(WM_MESSAGE_JJ,1003)
ID_DECLARE(WM_MESSAGE_KK,1002)
------------------------------------------------------
b.h
------------------------------------------------------
#pragma once
#include "id_system.h"
ID_DECLARE(WM_MESSAGE_PP,2013)
ID_DECLARE(WM_MESSAGE_TT,2014)
ID_DECLARE(WM_MESSAGE_VV,2015)

------------------------------------------------------
id_system.cpp
------------------------------------------------------
#define ID_CHECKER(classname) \
const unsigned int classname::MYID_TYPE::m_duplicate_checker=classname::MYID_TYPE::_ID; \
const unsigned int classname::ID = classname::MYID_TYPE::m_duplicate_checker; \


#include "a.h"
#include "b.h"

ID_CHECKER(WM_MESSAGE_KK)
ID_CHECKER(WM_MESSAGE_JJ)
ID_CHECKER(WM_MESSAGE_PP)
ID_CHECKER(WM_MESSAGE_TT)
ID_CHECKER(WM_MESSAGE_VV)

------------------------------------------------------
main.cpp
------------------------------------------------------
#include "a.h"
void main(){

    int x = WM_MESSAGE_KK::ID;
    int y = WM_MESSAGE_JJ::ID;
}

advantage: 1) It can detect duplicate id 2) client code (like main.cpp) need not include a big .h file. 3) compile time reduced greatly duo to smallest dependence .h file

thomas
  • 505
  • 3
  • 12