How can I refactor this to use an inline function or template instead of a macro?

Posted by BillyONeal on Stack Overflow See other posts from Stack Overflow or by BillyONeal
Published on 2010-03-22T04:26:49Z Indexed on 2010/03/22 4:31 UTC
Read the original article Hit count: 343

Filed under:
|

Hello, everyone :)

I have a useful macro here:

#define PATH_PREFIX_RESOLVE(path, prefix, environment) \
if (boost::algorithm::istarts_with(path, prefix)) { \
    ExpandEnvironmentStringsW(environment, buffer, MAX_PATH); \
    path.replace(0, (sizeof(prefix)/sizeof(wchar_t)) - 1, buffer); \
    if (Exists(path)) return path; \
}

It's used about 6 times within the scope of a single function (that's it), but macros seem to have "bad karma" :P

Anyway, the problem here is the sizeof(prefix) part of the macro. If I just replace this with a function taking a const wchar_t[], then the sizeof() will fail to deliver expected results.

Simply adding a size member doesn't really solve the problem either. Making the user supply the size of the constant literal also results in a mess of duplicated constants at the call site.

Any ideas on this one?

© Stack Overflow or respective owner

Related posts about c++

Related posts about macros