c++ - Implicit Template Parameters -
the following code generates compile error in xcode:
template <typename t> struct foo { foo(t value) { } }; int main() { foo myfoo(123); return 0; }
error: missing template arguments before 'myfoo'
changing foo myfoo(123);
foo<int> myfoo(123);
fixes issue, shouldn't compiler able figure out appropriate datatype?
is compiler bug, or misunderstanding implicit template parameters?
the constructor in theory infer type of object constructing, statement:
foo myfoo(123);
is allocating temporary space myfoo
, must know fully-qualified type of myfoo
in order know how space needed.
if want avoid typing (i.e. fingers) name of particularly complex template, consider using typedef
:
typedef std::map<int, std::string> stringmap;
or in c++0x use auto
keyword have compiler use type inference--though many argue leads less readable , more error-prone code, myself among them. ;p
Comments
Post a Comment