Illegal Token on Right Side of ::

Illegal token on right side of ::

My guess is that max has been made a macro. This happens at some point inside windows.h.

Define NOMINMAX prior to including to stop windows.h from doing that.

EDIT:

I'm still confident this is your problem. (Not including <limits> would result in a different error). Place #undef max and #undef min just before the function and try again. If that fixes it, I was correct, and your NOMINMAX isn't being defined properly. (Add it as a project setting.)

You can also prevent macro expansion by: (std::numeric_limits<T>::max)().


On a side note, why not do std::numeric_limits<T>::min() instead of negating the max?

Problem calling std::max

You are probably including windows.h somewhere, which defines macros named max and min.

You can #define NOMINMAX before including windows.h to prevent it from defining those macros, or you can prevent macro invocation by using an extra set of parentheses:

column = (std::max)(1u, column + count);

Why do I get an illegal token compile-time error with this piece of C++ code?

It looks like it's caused by this bug in Visual Studio:

https://connect.microsoft.com/VisualStudio/feedback/details/583081/

See also here:

template function specialization default argument

Why is std::min failing when windows.h is included?

The windows.h header file (or more correctly, windef.h that it includes in turn) has macros for min and max which are interfering.

You should #define NOMINMAX before including it.

Getting numerous errors with Vulkan Memory Allocator, '(': illegal token on right side of '::'

The fix is quite easy, all you must do is call the #include "vk_mem_alloc.h" before the #include <windows.h>, but it's something that an amateur like me can get easily hung up on, so I thought I'd document my difficulty here for others attempting to learn this daunting API.

Syntax error with std::numeric_limits::max

Your problem is caused by the <Windows.h> header file that includes macro definitions named max and min:

#define max(a,b) (((a) > (b)) ? (a) : (b))

Seeing this definition, the preprocessor replaces the max identifier in the expression:

std::numeric_limits<size_t>::max()

by the macro definition, eventually leading to invalid syntax:

std::numeric_limits<size_t>::(((a) > (b)) ? (a) : (b))

reported in the compiler error: '(' : illegal token on right side of '::'.

As a workaround, you can add the NOMINMAX define to compiler flags (or to the translation unit, before including the header):

#define NOMINMAX   

or wrap the call to max with parenthesis, which prevents the macro expansion:

size_t maxValue_ = (std::numeric_limits<size_t>::max)()
// ^ ^

or #undef max before calling numeric_limits<size_t>::max():

#undef max
...
size_t maxValue_ = std::numeric_limits<size_t>::max()


Related Topics



Leave a reply



Submit