-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
explicit constructor with default does not compile #3077
Comments
Do you have a complete example? |
This will repro the issue. It seems to be related to when a vector of a class contains a class that has an explicit constructor. I can probably work around this... but removing the {} from ValueType ret{} also fixes the problem, but I'm sure it's needed in some cases. #include "nlohmann/json.hpp"
class FooAlloc
{};
class Foo
{
public:
explicit Foo(const FooAlloc& = FooAlloc()) : value(false) {}
bool value;
};
class FooBar
{
public:
Foo foo;
};
inline void from_json(const nlohmann::json& j, FooBar& fb) { j.at("value").get_to(fb.foo.value); }
void test()
{
nlohmann::json j;
std::vector<FooBar> foo;
j.at("foo").get_to(foo);
} |
I can confirm the issue with Xcode on macOS, so it's not an MSVC issue. |
Interestingly, the json/include/nlohmann/json.hpp Lines 3035 to 3040 in 4b1cb9e
The change was made in #2561 with 1101f0e, apparently to silence a warning from Clang-Tidy. I was not aware that this could break code like this. I will add your example as regression test and revert the change and check if the CI passes. |
In fact, this is the Clang-Tidy warning that led to the (breaking) change:
|
Can you use |
PR #3079 is ready for review. Feedback greatly appreciated! |
* ⬆️ Doctest 2.4.7 * 👷 add CI step for ICPC * 👷 add CI step for ICPC * 👷 add CI step for ICPC * ⬇️ downgrade to Doctest 2.4.6 * 👷 add CI step for ICPC * 👷 add CI step for ICPC * 👷 add CI step for ICPC * 👷 add CI step for ICPC * 👷 add CI step for ICPC * 🔇 suppress warning #2196: routine is both "inline" and "noinline" * Re-enable <filesystem> detection on ICPC * Limit regression test for #3070 to Clang and GCC >=8.4 * Disable deprecation warnings on ICPC * Disable regression test for #1647 on ICPC (C++20) * Fix compilation failure of regression test for #3077 on ICPC * Disable wstring unit test on ICPC Fixes: error 913: invalid multibyte character sequence * Add ICPC to README Co-authored-by: Niels Lohmann <mail@nlohmann.me>
From include/nlohmann/json.hpp
The change from ValueType ret; to ValueType ret{}; is causing a compiler error for the construction of a type with an explicit default constructor of the form.
explicit Foo(const alloc_context& allocContext = alloc_context())
In vs2019 it gives the following errors, using c++17
error C2512: '###': no appropriate default constructor available
note: Constructor for class '###' is declared 'explicit'
This is happening in a from_json call to my custom container type
Worked in 3.9.1, does not work in 3.10.2
The text was updated successfully, but these errors were encountered: