0

if I declare a string and attempt to print it like this:

std::string s;
eosio::print(s);

the EOS compiler pukes:

In file included from t.cpp:1: In file included from ./t.hpp:4: In file included from /usr/local/Cellar/eosio.cdt/1.4.1/opt/eosio.cdt/bin/../include/eosiolib/asset.hpp:3: /usr/local/Cellar/eosio.cdt/1.4.1/opt/eosio.cdt/bin/../include/eosiolib/print.hpp:237:9: error: no member named 'print' in 'std::__1::basic_string' t.print(); ~ ^ t.cpp:126:3: note: in instantiation of function template specialization 'eosio::print &>' requested here print(ret); ^ 1 error generated.

but if I convert it:

eosio::print(std::string(s));

it works fine. why is this necessary? s is already a string so why do I need this?

1 Answer 1

1

As the compiler indicates

.../include/eosiolib/print.hpp:237:9: error: no member named 'print' in 'std::__1::basic_string' t.print(); ...

after check the the source code of print.hpp


inline void print( const std::string& s) {
  prints_l( s.c_str(), s.size() );
}

inline void print( std::string&& s) {
  prints_l( s.c_str(), s.size() );
}

...

template<typename T>
inline void print( T&& t ) {
   t.print();
}

then you will see that when you use

std::string s;
eosio::print(s);

it matches the print( T&& t) method best, so that will call the string.print which is not exist.

whereas use the

eosio::print(std::string(s));   // or eosio::print(std::move(s));

it matches the print( std::string&& s)

5
  • thank you for the explanation. I may need some clarification... std::string &&s reads to me like s is the address of the address of a std::string -- is that correct or does it mean something else? if so, why would that be matched to std::string(s), which is merely an object s of type std::string?
    – ekkis
    Commented Jan 25, 2019 at 3:42
  • there is some logic to what you're saying though, because I can also do char *a = &(s[0]); print("%s\n", a); and that compiles fine... so that matches what?
    – ekkis
    Commented Jan 25, 2019 at 3:55
  • std::string &&s is a rvalue reference, not address of the address, and std::string& is lvalue reference, You can read 《C++ Primer》 for more.
    – Reason Lee
    Commented Jan 25, 2019 at 4:01
  • why char* is fine, because it has a char * function overloading (github.com/EOSIO/eosio.cdt/blob/…),this is all about C++ language, not the EOS
    – Reason Lee
    Commented Jan 25, 2019 at 4:03
  • I'm having to reacquaint myself with this ancient and arcane language. it has changed at lot in 30 years since I touched it, but thanks :)
    – ekkis
    Commented Jan 25, 2019 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.