Advertisement

Compiler issue

Started by March 05, 2025 08:52 AM
2 comments, last by Angels 1 day, 3 hours ago

I have a codebase that I have not touched in quite a while like 5 months ago after finding a better alternative, but now I wanna experiment with it agian, the problem is nothing changed and it used to compile just fine, I'm in a bit of a situation now, the error I am  getting is:

syntax error: unexpected token '*' following 'expression'

for this piece of code:

template<class T>
constexpr [[nodiscard]] T Square(T value) requires std::is_arithmetic_v<T>
{
   return T * T;
}
 

it worked before but now I am getting errors around it, I have not  changed anything, using Preview - Features from the Latest C++ Working Draft (/std:c++latest) as well

my settings:

 

Has anything changed in the C++ standard or in how compilers handle this kind of code recently? Any idea how to make it work with the latest version?

Appreciate any help and insights

None

Angels said:
it worked before but now I am getting errors around it, I have not changed anything, using Preview - Features from the Latest C++ Working Draft (/std:c++latest) as well

Your code is broken. You are trying to multiply two types - "T *" T", where T is the template type. You probably mean “value * value”, The reason why this may have worked before is that, previous versions for MSVC where non-compliant with the standard, and didn't check a template function for basic syntax errors unless it was instantiated. So you certainly never use this function in your code (because it could never, ever successfully compile under any circumstances), so before the update MSVC ignored it. Now, MSVC is required by the standard to check template functions for certain types of errors, that are independant of the actual template arguments (which, trying to value-operations onto template-types is one of). This leads to the situation. The fix is, as I initially stated, to fix the function by multiplying the “value” variable, that is passed to the function instead.

Advertisement

@Juliean Thanks a lot, Julian! I loveod your explanation! That makes perfect sense! I corrected the code and it's compiling now, thank you again.

None

Advertisement