Tracktion / choc

A collection of header only classes, permissively licensed, to provide basic useful tasks with the bare-minimum of dependencies.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`choc::json::writeAsJSON` doesn't compile with C++ 20 on MSVC

peter1745 opened this issue · comments

Hello! Just wanted to let you know that there's an issue when including choc_JSON.h if you're using C++ 20 on MSVC. It doesn't compile because choc::json::writeAsJSON defines a Writer struct in the function, not entirely sure why but it could be because of the function being templated.

What I did locally was move the Writer struct to just above the function and made it templated so the new code is:

//==============================================================================
template<typename Stream>
struct Writer
{
	Stream& out;

	void dump(const value::ValueView& v)
	{
		if (v.isVoid()) { out << "null"; return; }
		if (v.isString()) { out << getEscapedQuotedString(v.getString()); return; }
		if (v.isBool()) { out << (v.getBool() ? "true" : "false"); return; }
		if (v.isFloat()) { out << doubleToString(v.get<double>()); return; }
		if (v.isInt()) { out << v.get<int64_t>(); return; }
		if (v.isObject())                 return dumpObject(v);
		if (v.isArray() || v.isVector())  return dumpArrayOrVector(v);
	}

	void dumpArrayOrVector(const value::ValueView& v)
	{
		out << '[';
		auto num = v.size();

		for (uint32_t i = 0; i < num; ++i)
		{
			if (i != 0) out << ", ";
			dump(v[i]);
		}

		out << ']';
	}

	void dumpObject(const value::ValueView& object)
	{
		out << '{';
		auto numMembers = object.size();

		for (uint32_t i = 0; i < numMembers; ++i)
		{
			if (i != 0) out << ", ";

			auto member = object.getObjectMemberAt(i);
			out << getEscapedQuotedString(member.name) << ": ";
			dump(member.value);
		}

		out << '}';
	}
};


template <typename Stream>
void writeAsJSON (Stream& output, const value::ValueView& value)
{
    Writer<Stream>{ output }.dump (value);
}

Like I said I'm not sure why this is works but not the old one, it could of course be our project settings that causes issues but either way just wanted to let you know that this is an issue I had!

Thanks for the heads-up! That actually looks like a compiler bug to me, but working around it makes sense.. Surprised to find that when I tried C++20 there were a bunch of other stupid things too, so I've fixed them and updated the tests to run C++20 mode for MSVC to catch any others..