c++ - Template function argument as 'const T' -
i given template function member function in class compactwriter
:
template<typename t> compactwriter operator%(const field<const t>& field) { _os << field.getname() << ": " << field.valuereference() << ", "; return *this; }
and then, asked second const for.
know according : is there difference between “t” , “const t” in template parameter? there should no difference.
therefore, i'm kind of confused of why question asked.
a little background hoping might trigger might have missed...
template class 'field':
#ifndef serialization_field_h #define serialization_field_h #include <string> #include <iostream> template<typename t> class field { private: public: field(const std::string& name, t& value) : _name(name) , _value(value) {} const std::string& getname() const { return _name; } t& valuereference() const { return _value; } private: std::string _name; t& _value; }; template<typename t> const field<t> field(const std::string& name, t& value) { return field<t>(name, value); } #endif //serialization_field_h
and operator calls made in main.cpp
:
const std::string name = "dan"; const int age = 35; const double weight = 30 + sqrt(2); compactwriter writer(std::cout); // initialize writer object. writer.open(); writer % field("name", name) % field("age", age) % field("weight", weight); writer.close(); return 0;
the functions writer.open()
, writer.close()
nothing except adding cout {
, }
.
if point me reason why second const
in operator overload %
member function header there, great.
that question being asked in completely different context. const
have here significant. otherwise, not legal use fields of const
template parameter type.
Comments
Post a Comment