Skip to content

Troubles with using into_toml member function #255

@CDK6182CHR

Description

@CDK6182CHR

Recently I have encountered some trouble when try to use the into_toml member function to convert a user-defined struct to toml::value, using the method described in the document: link.

Minimal example

(See also the file attached)
toml11_into_toml_test.zip

Here I defined a struct with into_toml member function:

struct Config {
    int x = 1;
    double y = 2.;

    template <typename TC>
    toml::basic_value<TC> into_toml() const 
    {
         return toml::basic_value<TC>(typename toml::basic_value<TC>::table_type{
                {"x", this->x}, {"y", this->y}
            });
    }
};

Then I tried to convert it into toml::value using codes like this:

toml::value v;
v["config"] = c;

Then I get compiling error.

I found also that the type trait below gives a false value:

constexpr const auto t = toml::detail::has_into_toml_method<Config>::value;   // false

The implementation of such trait is (in V4.0.3 code, traits.hpp:81):

struct has_into_toml_method_impl
{
    template<typename T>
    static std::true_type  check(decltype(std::declval<T>().into_toml())*);
    // Other code omitted
};

And this is called similarly in the constructor of toml::basic_value.
It seems that the template argument TC (see the definition above) is not passed into the function into_toml, and the compiler has no way to deduce the TC parameter.
This may be the cause of such problem, to the best of my understanding.

On the other hand: the specialization of toml::into works well for this.

I wonder whether there is something wrong with my implementation (the definition of into_toml member function, etc), or we cannot use the into_toml member function in this case?

Test environments

The tests have been performed in the following environments and yield similar results:

  • On Windows: MSVC 19.33, C++20, TOML11 V4.0.3;
  • On Linux: GCC 14.1.0, C++20 and C++23, TOML11 V4.0.1

Thank you in advance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions