c++11 - Code explanation of the json11 library about implicit constructor -


i'm reading source code of main json11 header file.

it contains following declaration:

template <class t, class = decltype(&t::to_json)> json(const t & t) : json(t.to_json()) {} 

i'm trying find documentation usage of decltype , class inside template declaration no success.

does construction/usage has name in c++? reference it?

it's using sfinae ("substitution failure is not an error"), common technique advanced template stuff. in case, it's used crude(1) test whether type t has function named to_json.

how works: if expression t::to_json well-formed (there named to_json inside type t), decltype(t::to_json) denotes valid type , constructor template can used normally.

however, if t::to_json ill-formed (i.e. if there no to_json member inside t), means substituting template argument t has failed. per sfinae, not error of entire program; means template removed further consideration (as if never part of class).

the effect if type t has member to_json, can use object of type t initialise json object. if there no such member in t, constructor not exist.


(1) i'm saying crude test because checks t has such member. doesn't check member function can invoked without arguments , returns constructor of json can accept.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -