title: When deserializing a struct with from_json: error: no matching function for call to nlohmann::basic_json<>::get
body: <p>I'm using the <code>nlohmann/json</code> library and trying to implement serialization and deserialization for one of my structs. I'm going by <a href="https://github.com/nlohmann/json#arbitrary-types-conversions" rel="nofollow noreferrer">this example</a>.</p>
  <p>This is the struct and the relevant types:</p>
  <pre class="lang-cpp prettyprint-override"><code>typedef std::uint32_t vertex_key;
  typedef std::uint64_t edge_key;
  inline edge_key get_edge_key(vertex_key v1, vertex_key v2) {
      return (std::uint64_t)v1 &lt;&lt; 32 | (std::uint64_t)v2;  // edge 64b = (v1 32b, v2 32b)
  }
  
  struct Mcnfmq {
      unsigned int n_nodes;
      vertex_key source;
      vertex_key sink;
      unsigned int flow_value;
  
      std::unordered_map&lt;edge_key, int&gt; costs;
      std::unordered_map&lt;edge_key, int&gt; capacities;
      std::unordered_map&lt;edge_key, int&gt; minimum_quantities;
  
      Mcnfmq(unsigned int n_nodes, vertex_key source, vertex_key sink, unsigned int flow_value)
          : n_nodes(n_nodes), source(source), sink(sink), flow_value(flow_value) {}
  };
  
  void to_json(json&amp; j, const Mcnfmq&amp; instance);
  void from_json(const json&amp; j, Mcnfmq&amp; instance);
  </code></pre>
  <p>Here are the to/from implementations:</p>
  <pre class="lang-cpp prettyprint-override"><code>void to_json(json&amp; j, const Mcnfmq&amp; instance) {
      j = json{
          {&quot;n_nodes&quot;, instance.n_nodes},
          {&quot;source&quot;, instance.source},
          {&quot;sink&quot;, instance.sink},
          {&quot;flow_value&quot;, instance.flow_value},
          {&quot;costs&quot;, instance.costs},
          {&quot;capacities&quot;, instance.capacities},
          {&quot;minimum_quantities&quot;, instance.minimum_quantities},
      };
  }
  
  void from_json(const json&amp; j, Mcnfmq&amp; instance) {
      j.at(&quot;n_nodes&quot;).get_to(instance.n_nodes);
      j.at(&quot;source&quot;).get_to(instance.source);
      j.at(&quot;sink&quot;).get_to(instance.sink);
      j.at(&quot;flow_value&quot;).get_to(instance.flow_value);
      j.at(&quot;costs&quot;).get_to(instance.costs);
      j.at(&quot;capacities&quot;).get_to(instance.capacities);
      j.at(&quot;minimum_quantities&quot;).get_to(instance.minimum_quantities);
  }
  </code></pre>
  <p>The struct <code>Mcnfmq</code> and functions <code>to_json</code> and <code>from_json</code> obviously reside in the same namespace (global), so that shouldn't be an issue.</p>
  <p>This is the main file:</p>
  <pre class="lang-cpp prettyprint-override"><code>int main() {
      std::ifstream i(&quot;output.json&quot;);
      json j2;
      i &gt;&gt; j2;
      i.close();
  
      Mcnfmq instance2 = j2.get&lt;Mcnfmq&gt;(); // ERROR HERE
  
      return 0;
  }
  </code></pre>
  <p>Full error output:</p>
  <pre><code>test_serialization.cpp: In function 'int main()':
  test_serialization.cpp:27:38: error: no matching function for call to 'nlohmann::basic_json&lt;&gt;::get&lt;Mcnfmq&gt;()'
     27 |     Mcnfmq instance2 = j2.get&lt;Mcnfmq&gt;();
        |                        ~~~~~~~~~~~~~~^~
  In file included from test_serialization.cpp:6:
  json.hpp:20717:10: note: candidate: 'template&lt;class ValueTypeCV, class ValueType&gt; constexpr decltype (declval&lt;const basic_json_t&amp;&gt;().get_impl&lt;ValueType&gt;(nlohmann::detail::priority_tag&lt;4&gt;{})) nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get() const [with ValueTypeCV = ValueTypeCV; ValueType = ValueType; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20717 |     auto get() const noexcept(
        |          ^~~
  json.hpp:20717:10: note:   template argument deduction/substitution failed:
  json.hpp: In substitution of 'template&lt;class ValueTypeCV, class ValueType&gt; constexpr decltype (declval&lt;const basic_json_t&amp;&gt;().get_impl&lt;ValueType&gt;(nlohmann::detail::priority_tag&lt;4&gt;{})) nlohmann::basic_json&lt;&gt;::get&lt;ValueTypeCV, ValueType&gt;() const [with ValueTypeCV = Mcnfmq; ValueType = Mcnfmq]':
  test_serialization.cpp:27:38:   required from here
  json.hpp:20719:81: error: no matching function for call to 'nlohmann::basic_json&lt;&gt;::get_impl&lt;Mcnfmq&gt;(nlohmann::detail::priority_tag&lt;4&gt;) const'
  20719 |     -&gt; decltype(std::declval&lt;const basic_json_t&amp;&gt;().template get_impl&lt;ValueType&gt;(detail::priority_tag&lt;4&gt; {}))
        |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  json.hpp:20579:15: note: candidate: 'template&lt;class ValueType, typename std::enable_if&lt;(nlohmann::detail::is_default_constructible&lt;T1&gt;::value &amp;&amp; nlohmann::detail::has_from_json&lt;nlohmann::basic_json&lt;&gt;, ValueType, void&gt;::value), int&gt;::type &lt;anonymous&gt; &gt; ValueType nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get_impl(nlohmann::detail::priority_tag&lt;0&gt;) const [with ValueType = ValueType; typename std::enable_if&lt;(nlohmann::detail::is_default_constructible&lt;ValueType&gt;::value &amp;&amp; nlohmann::detail::has_from_json&lt;nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;, ValueType&gt;::value), int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20579 |     ValueType get_impl(detail::priority_tag&lt;0&gt; /*unused*/) const noexcept(noexcept(
        |               ^~~~~~~~
  json.hpp:20579:15: note:   template argument deduction/substitution failed:
  json.hpp:20578:28: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20578 |                    int &gt; = 0 &gt;
        |                            ^
  json.hpp:20621:15: note: candidate: 'template&lt;class ValueType, typename std::enable_if&lt;nlohmann::detail::has_non_default_from_json&lt;nlohmann::basic_json&lt;&gt;, ValueType, void&gt;::value, int&gt;::type &lt;anonymous&gt; &gt; ValueType nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get_impl(nlohmann::detail::priority_tag&lt;1&gt;) const [with ValueType = ValueType; typename std::enable_if&lt;nlohmann::detail::has_non_default_from_json&lt;nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;, ValueType&gt;::value, int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20621 |     ValueType get_impl(detail::priority_tag&lt;1&gt; /*unused*/) const noexcept(noexcept(
        |               ^~~~~~~~
  json.hpp:20621:15: note:   template argument deduction/substitution failed:
  json.hpp:20620:28: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20620 |                    int &gt; = 0 &gt;
        |                            ^
  json.hpp:20646:19: note: candidate: 'template&lt;class BasicJsonType, typename std::enable_if&lt;nlohmann::detail::is_basic_json&lt;BasicJsonType&gt;::value, int&gt;::type &lt;anonymous&gt; &gt; BasicJsonType nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get_impl(nlohmann::detail::priority_tag&lt;2&gt;) const [with BasicJsonType = BasicJsonType; typename std::enable_if&lt;nlohmann::detail::is_basic_json&lt;BasicJsonType&gt;::value, int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20646 |     BasicJsonType get_impl(detail::priority_tag&lt;2&gt; /*unused*/) const
        |                   ^~~~~~~~
  json.hpp:20646:19: note:   template argument deduction/substitution failed:
  json.hpp:20645:28: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20645 |                    int &gt; = 0 &gt;
        |                            ^
  json.hpp:20669:16: note: candidate: 'template&lt;class BasicJsonType, typename std::enable_if&lt;std::is_same&lt;BasicJsonType, nlohmann::basic_json&lt;&gt; &gt;::value, int&gt;::type &lt;anonymous&gt; &gt; nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt; nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get_impl(nlohmann::detail::priority_tag&lt;3&gt;) const [with BasicJsonType = BasicJsonType; typename std::enable_if&lt;std::is_same&lt;BasicJsonType, nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt; &gt;::value, int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20669 |     basic_json get_impl(detail::priority_tag&lt;3&gt; /*unused*/) const
        |                ^~~~~~~~
  json.hpp:20669:16: note:   template argument deduction/substitution failed:
  json.hpp:20668:25: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20668 |                  int&gt; = 0&gt;
        |                         ^
  json.hpp:20682:20: note: candidate: 'template&lt;class PointerType, typename std::enable_if&lt;std::is_pointer&lt;_Tp&gt;::value, int&gt;::type &lt;anonymous&gt; &gt; constexpr decltype (declval&lt;const basic_json_t&amp;&gt;().get_ptr&lt;PointerType&gt;()) nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get_impl(nlohmann::detail::priority_tag&lt;4&gt;) const [with PointerType = PointerType; typename std::enable_if&lt;std::is_pointer&lt;_Ptr&gt;::value, int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20682 |     constexpr auto get_impl(detail::priority_tag&lt;4&gt; /*unused*/) const noexcept
        |                    ^~~~~~~~
  json.hpp:20682:20: note:   template argument deduction/substitution failed:
  json.hpp:20681:25: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20681 |                  int&gt; = 0&gt;
        |                         ^
  json.hpp:20758:10: note: candidate: 'template&lt;class PointerType, typename std::enable_if&lt;std::is_pointer&lt;_Tp&gt;::value, int&gt;::type &lt;anonymous&gt; &gt; decltype (declval&lt;nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::basic_json_t&amp;&gt;().get_ptr&lt;PointerType&gt;()) nlohmann::basic_json&lt;ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType&gt;::get() [with PointerType = PointerType; typename std::enable_if&lt;std::is_pointer&lt;_Ptr&gt;::value, int&gt;::type &lt;anonymous&gt; = &lt;anonymous&gt;; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string&lt;char&gt;; BooleanType = bool; NumberIntegerType = long long int; NumberUnsignedType = long long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector&lt;unsigned char&gt;]'
  20758 |     auto get() noexcept -&gt; decltype(std::declval&lt;basic_json_t&amp;&gt;().template get_ptr&lt;PointerType&gt;())
        |          ^~~
  json.hpp:20758:10: note:   template argument deduction/substitution failed:
  json.hpp:20757:68: error: no type named 'type' in 'struct std::enable_if&lt;false, int&gt;'
  20757 |                  std::is_pointer&lt;PointerType&gt;::value, int&gt;::type = 0&gt;
        |                                                                    ^
  </code></pre>
  <p>My questions:</p>
  <ol>
  <li>What are the error messages actually saying?</li>
  <li>How can this be fixed?</li>
  </ol>