Skip to content

Compilation error with MSVC since 2.5.0 #295

@addy90

Description

@addy90

Hi there,

there is a compilation error of csv-parser since version 2.5.0 with MSVC, see this example: https://godbolt.org/z/jea4fjf8e

example.cpp
C:\Windows\TEMP\compiler-explorer-compiler8BP4DR\csv.hpp(3280): error C2039: 'result_of': is not a member of 'std'
Z:\compilers\msvc\14.44.35207-14.44.35219.0\include\unordered_set(23): note: see declaration of 'std'
C:\Windows\TEMP\compiler-explorer-compiler8BP4DR\csv.hpp(3280): error C2653: 'result_of': is not a class or namespace name
C:\Windows\TEMP\compiler-explorer-compiler8BP4DR\csv.hpp(3280): error C2061: syntax error: identifier 'type'
Compiler returned: 2

The error was introduced with #290

Replaced std::result_of (removed in C++20) with a csv::internals::invoke_result_t alias that selects std::invoke_result on C++17+ and std::result_of on older standards.

The issue arises from this line:

using invoke_result_t = typename std::result_of<F(Args...)>::type;

The problem comes from here:

#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 17) || __cplusplus >= 201703L

The issue is that MSVC sets __cplusplus to 199711L by default, unless /Zc:__cplusplus is supplied: https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170

One idea to fix this issue is to use _MSVC_LANG in addition, see: https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
(search for _MSVC_LANG)

One solution could be to use this code instead for the lines starting from

#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 20) || __cplusplus >= 202002L

Here is the provided solution, it also takes into account when no CMake is used and the CMAKE_CXX_STANDARD variable is not set:

#if defined(CMAKE_CXX_STANDARD)
    #define CSV_CMAKE_CXX_STANDARD CMAKE_CXX_STANDARD
#else
    #define CSV_CMAKE_CXX_STANDARD 0
#endif

#if defined(_MSVC_LANG)
    #define CSV_COMPILER_CXX_LEVEL _MSVC_LANG
#else
    #define CSV_COMPILER_CXX_LEVEL __cplusplus
#endif

#if CSV_CMAKE_CXX_STANDARD >= 20 || CSV_COMPILER_CXX_LEVEL >= 202002L
    #define CSV_HAS_CXX20
#endif

#if CSV_CMAKE_CXX_STANDARD >= 17 || CSV_COMPILER_CXX_LEVEL >= 201703L
    #define CSV_HAS_CXX17
#endif

#if CSV_CMAKE_CXX_STANDARD >= 14 || CSV_COMPILER_CXX_LEVEL >= 201402L
    #define CSV_HAS_CXX14
#endif

In the example above, I also included this fix as a test, when you include the csv-fixed.hpp instead of the default csv.hpp it works in all compilers without the /Zc:__cplusplus flag.

Feel free to adapt the solution suggestion!

Thank you!

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions