marzer / tomlplusplus

Header-only TOML config file parser and serializer for C++17.
https://marzer.github.io/tomlplusplus/
MIT License
1.53k stars 146 forks source link

Lambda function error using nvcc #213

Closed bfstree closed 6 months ago

bfstree commented 10 months ago

Environment

toml++ version and/or commit hash:
toml++ v3.4.0

Compiler:
nvcc v12.1 and v11.6

C++ standard mode:
-std=c++20 and c++17

Target arch:
x86_64

Library configuration overrides:
None

Relevant compilation flags:
--extended-lambda -std=c++17 or c++20

Describe the bug

Does not compile with error:

toml.hpp: In lambda function:
toml.hpp:8127:123: error: ‘__T0’ was not declared in this scope; did you mean ‘__y0’?
 8127 |                                    if constexpr (impl::is_constructible_or_convertible<bool, return_type>)
      |                                                                                      ^~~~
      |                                                                                      __y0

Steps to reproduce (or a small repro code sample)

$cat main.cu
#include <cuda.h>
#include <cstdio>
#include "toml.hpp"
int main(int argc, char **argv) {
  printf("hello world!\n");
  return 0;
}

Additional information

Compile the above main.cu file using nvcc --extended-lambda -std=c++17 main.cu c++17 and c++20 both produces the same error, also with or without --extended-lambda

There is some documentation here: https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#extended-lambdas and: https://stackoverflow.com/questions/74349712/cuda-11-fails-to-compile-due-to-type-traits-in-standard-library-when-using-exten

marzer commented 10 months ago

O_O that is a crazy bug. Thanks for doing the legwork to look into the underlying issue! I don't know when I'll get time to action this, but in the short-term there's a workaround you can try:

In an old version of GCC there was a bug on those branches (relating to detecting the return type of a lambda in a template), and I added a fallback escape hatch that just happens to disable the problematic code causing your issue:

#define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN 1
#define TOML_RETURN_BOOL_FROM_FOR_EACH_BROKEN_ACKNOWLEDGED 1
#include "toml.hpp"

It means you can't use a bool (or bool-convertible) type returning false to early exit from a for_each() method, but it might at least unblock you. (See #197 if you want some more context for this workaround.)

bfstree commented 10 months ago

This workaround works for now. Now, I can use toml instead of complex command line arguments and export runtime statistics, which simplifies and integrates well into the entire development process. Thank you!