Skip to content
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

Custom conversion doesn't work #83

Closed
htrefil opened this issue Oct 3, 2019 · 2 comments
Closed

Custom conversion doesn't work #83

htrefil opened this issue Oct 3, 2019 · 2 comments

Comments

@htrefil
Copy link

htrefil commented Oct 3, 2019

I tried to define a custom specialization of toml::from for my type, but it ends up with a compile time error.

Here's the code:

#include "toml11/toml.hpp"

enum class animal {
	DOG,
	CAT,
};

namespace toml {

template<>
struct from<animal> {
	animal from_toml(const value& value) {
		return animal::DOG;
	}
};

}

int main(int argc, char** argv) {
	auto data = toml::parse(argv[1]);
	auto a = toml::find<animal>(data, "a");
}

Apart from a bunch of template substitution failure errors, I get

toml11/toml/get.hpp:415:3: note: template argument deduction/substitution failed:
toml11/toml/get.hpp:259:26: error: invalid application of ‘sizeof’ to incomplete type ‘toml::into’
std::size_t S = sizeof(::toml::into)>
^~~~~~~~~~~~~~~~~~~~~~~
toml11/toml/get.hpp:259:48: error: could not convert template argument ‘’ from ‘’ to ‘long unsigned int’
std::size_t S = sizeof(::toml::into)>

Compiled with g++ 8.3.0 with -std=c++17.
Using the latest toml11 cloned from GitHub.

Is this a bug or am I doing something wrong?

@jcmoyer
Copy link
Contributor

jcmoyer commented Oct 4, 2019

I think the documentation is incorrect (from_toml should be static), see this test for correct usage: https://github.com/ToruNiina/toml11/blob/master/tests/test_extended_conversions.cpp#L38-L57

EDIT:
After a bit of testing, I've determined that there's another issue where you have to specialize both toml::into and toml::from for your type, even if you're only reading toml. I'm not sure if this is intended or a bug.

@ToruNiina
Copy link
Owner

Thank you @htrefil for the report!
And thank you @jcmoyer for the PR and finding out the problem!

This is a bug.
As you pointed out, there is a defect in the code and it requires conversion in both directions even if only one direction is needed. I've just fixed that and will push it soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants