Thalhammer / jwt-cpp

A header only library for creating and validating json web tokens in c++
https://thalhammer.github.io/jwt-cpp/
MIT License
875 stars 239 forks source link

Building example: no matching function for call to ‘decode(std::string&)’ #327

Closed bartoszmodelski closed 8 months ago

bartoszmodelski commented 8 months ago

What's your question?

No idea if this is expected (if so, apologies for the noise) or not, feel free to close. In any case, that should make the error googleable for other novices.

Additional Context

The basic example failed to compile for me:

error: no matching function for call to ‘decode(std::string&)’
    8 |     auto decoded = jwt::decode(token);
...
note: candidate: ‘template<class json_traits> jwt::decoded_jwt<json_traits> jwt::decode(const typename json_traits::string_type&)’
 3626 |         decoded_jwt<json_traits> decode(const typename json_traits::string_type& token) {
      |                                  ^~~~~~
note:   template argument deduction/substitution failed:
/home/model/crypto/mm/src/bin/main.cpp:8:31: note:   couldn’t deduce template parameter ‘json_traits’
    8 |     auto decoded = jwt::decode(token);

Fixed by linking nlohman_json (or other supported json libs, I presume) and including trait defaults: #include <jwt-cpp/traits/nlohmann-json/defaults.h>.

Thalhammer commented 8 months ago

jwt-cpp is built around the concept of having replaceable json/crypto libraries and as a result of that needs to know which one you are using. You can do this by either including the default.h of one of the traits (like you did, note that it implicitly includes the main header, so you only need default.h) or by explicitly specifying the traits as part of the decode/create functions (which is what the error is trying to tell you, you are missing the template parameter).

What the default.h headers do is include the traits and jwt header and declaring the trait as a default template parameter, which allows you to skip specifying it.

So yes, this is expected behaviour. It used to be different a long time ago when jwt-cpp relied exclusively on picojson and it used to include the picojson traits as a default for a while after (unless you explicitly disabled it), however for a couple of releases now this is how it works.