2

We allow to programmer to add a TODO message to code. Every time the file is compiled, message is written to the output (via pragma message). The TODO is a macro, containing a message and date when it was created. E.g. TODO(3, 8, 2015, "...");

I want to validate that no TODO is older than 30 days (roughly) and prevent compilation if there is such. (you can of course change date, but it will be clearly visible in version control)

I need solution, which works in MS VC 2012

I have written (based on other question) a version, which works fine on gcc - Here is working example but it doesn't compile on visual studio with error C2057 : expected constant expression message

#define TOTAL_DAYS(day,month,year) (day + month*31 + year*365)

namespace checkingDate
{
#define C(i) static const char c##i = __DATE__[i];

    C(0); C(1); C(2); C(3);
    C(4); C(5); C(6); C(7);
    C(8); C(9); C(10);

    static const int current_date = c4 == ' ' ? c5 - '0' : c5 - '0' + 10;

    static const int current_month = (
        c0 == 'J' // Jan Jun Jul
        ? (c1 == 'a' ? 1 : (c2 == 'n' ? 6 : 7))
        : c0 == 'F' ? 2
        : c0 == 'M' // Mar May
        ? (c2 == 'r' ? 3 : 5)
        : c0 == 'A' // Apr Aug
        ? (c1 == 'p' ? 4 : 8)
        : c0 == 'S' ? 9
        : c0 == 'O' ? 10
        : c0 == 'N' ? 11
        : 12
        );

    static const int current_year = ((c7 - '0') * 1000) + ((c8 - '0') * 100) + ((c9 - '0') * 10) + (c10 - '0');
    static const int totalDays = TOTAL_DAYS(current_date, current_month, current_year);
}

#   define TODO(day,month,year, str)  static_assert(checkingDate::totalDays - TOTAL_DAYS(day,month,year)<30, "old msg");

int main()
{
    TODO(3, 8, 2015, "...");
}

Can I somehow fix this error? Is it my error or MS VC bug? Is there other way to write it, so it works in visual studio?

Community
  • 1
  • 1
relaxxx
  • 7,566
  • 8
  • 37
  • 64
  • what line does msvc give you the bug at? – Chris Beck Sep 06 '15 at 16:19
  • on TODO(3, 8, 2015, "..."); where static_assert is – relaxxx Sep 06 '15 at 17:46
  • I don't have msvc so I can't check, but I would assume that if you do the arithmetic using templates it would work in msvc since that is an older feature than the static_assert, constexpr stuff. – Chris Beck Sep 06 '15 at 17:49
  • This boils down to to the fact that `__DATE__[i]` isn't accepted as constant expression by VS2012. Even `"Sep 6 2015"[0]` isn't taken as constant expression. And there seems to be no way of processing strings with the preprocessor. – honk Sep 06 '15 at 20:15

0 Answers0