简体   繁体   中英

C2975 invalid template argument for 'N', expected compile-time constant expression

Here is a kind of code snippet which supposed to run on the version about 7.1 (VS 2003).

I can not compile it on Visual Studio 2013.

.h header:

template <HINSTANCE h, DWORD hash, class N>
inline LPVOID testFunc(N n1)
{   
    ...
    return ret_func(n1);
}


.cpp:

HINSTANCE kernel32;

int WINAPI WinMain(...)
{
    ...
    kernel32 = GetKernel32();  
    HINSTANCE mod = testFunc<kernel32, 0x0BADC0DE>("some_string");  // C2975
    ...
}

error C2975: 'h' : invalid template argument for 'testFunc', expected compile-time constant expression

I tried like this:

HINSTANCE kernel32 = GetKernel32();  // implemented ok
HINSTANCE mod = testFunc<kernel32, 0x0BADC0DE>("some_string");  // C2971

error C2971: 'pushargEx' : template parameter 'h' : 'kernel32' : a local variable cannot be used as a non-type argument

The error is actually self-explanatory:

expected compile-time constant expression

Which kernel32 is NOT.

"Compile-time constant" means that the value must be known at the time of compilation, ie specified (directly or indirectly) in the source code itself.

kernel32 is not a compile time constant. It needs to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM