# C++ if constexpr, C++17

## Recommended Posts

Hey

So i have been tinkering alot with the features of C++17 and i think there are some really nice things that have been added.
But i ran into a problem.

template< class T >
void test() {
if constexpr( std::is_void_v<T> ) {
T * t;
} else {
T t;
}
}

int main() {
test< void >();
return 0;
}

Here i expect the compiler to tell me everything is ok and simply skip the else branch.
Since i never call the function "test" with something that isnt a void.
I assume i would only reach the point "T *t".

But thats not the case, im using the very latest version of Visual Studio, 15.5.5.
I have in my project enabled the C++17 language standard and also enabled the conformance mode (/permissive-)
Yet Visual Studio is telling me "illegal use of type 'void'" which of course is correct if i ever where to reach that branch.

Am i just not understanding how its supposed to work?
Any help would be greatly appreciated

##### Share on other sites

Running that code on the command-line (cl /std:c++latest test.cpp) and with the necessary type_traits include works perfectly fine.

Are you sure there's not some other error that's causing the branch to fail to compile?

##### Share on other sites

I dont see any other errors and the code i posted is the minimum i can boil it down to.
Im sure there is something, since im clearly getting the problem, but i just cant see what it could be

I enabled someform of more verbose build log and i see this as my build line.

cl /c /ZI /W3 /WX- /diagnostics:classic /sdl /Od /D _DEBUG /D _WINDOWS /D PMPLATFORM_WINDOWS /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /std:c++17 /Fo"x64\Debug\\" /Fd"x64\Debug\vc141.pdb" /Gd /TP /errorReport:prompt Src\Main.cpp

Kinda heavy i think but its the default basically with C++17 enabled.

edit: the command line there is from a windows project, i copied from the wrong project, but it dident work there either...

Edited by Noxil
Clarification on command line

##### Share on other sites

Try turning off SDL checks, and solve the warnings that can be raised up by this change

Edited by malium

##### Share on other sites

I tried that, and also i have tried to set the language to latest instead of C++17 to really ensure that the features are enabled at its fullest, but it dosnt seem to work.

##### Share on other sites

Okey so im at work right now, created a small test project and everything works fine.
So i assume there is something wrong in my environment...

##### Share on other sites

As a final note, i ran "Repair" from the Visual Studio installer.
Now everything seems to work. So there was indeed something fishy with my environment.

## Create an account

Register a new account

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632563
• Total Posts
3007103

×