MSVC 2017是否支持std::any?编译报错C2039如何解决?
Let's walk through resolving this error step by step—this is a common gotcha when working with C++17 features in MSVC.
First, let's break down why you're seeing this error: std::any is a C++17 standard library feature, so the issue likely ties to missing headers, an outdated compiler, or incorrect compile flags.
Step 1: Make sure you're including the required header
This is the easiest mistake to miss! To use std::any, your code must include the <any> header:
#include <any>
Without this include, even a fully C++17-compliant compiler won't recognize std::any.
Step 2: Verify your MSVC compiler version
MSVC didn't add full support for std::any until Visual Studio 2017 Update 3 (compiler version 19.11.x). If you're using an older version, even with /std:c++latest, the feature won't be available.
Check your compiler version by running this command in your terminal:
cl /Bv
Look for the version number in the output (e.g., 19.37.x for VS2022). If it's older than 19.11, you'll need to update Visual Studio to a newer release (VS2019 or 2022 are recommended for better C++ standard support).
Step 3: Ensure your compile flags correctly enable C++17
While /std:c++latest includes C++17 features, explicitly specifying /std:c++17 (or /std:c++20//std:c++23 for newer standards) can avoid edge cases with older MSVC versions. Update your compile command to:
cl /c /std:c++17 /Gm- /sdl /Zc:inline /RTC1 /Oy /MDd /FA /EHs main.cxx
Note: The /Oy flag (omit frame pointers) is typically ignored in debug mode (since you're using /MDd and /RTC1), but that doesn't affect your std::any error.
Step 4: Check for interfering macros
Occasionally, code or compile flags might define macros that disable C17 features. Look for things like _HAS_CXX17=0 in your code or compile arguments—if present, remove them to enable C17 support.
内容的提问来源于stack exchange,提问作者bMain

