Code example:
#include <iostream> #include <string> struct A { int a; explicit operator std::string() { return "<" + std::to_string(a) + ">"; } }; void f(std::string s) { std::cout << s << std::endl; } int main() { A a {1}; f(static_cast<std::string>(a)); f(std::string(a)); f((std::string)a); // f(boost::lexical_cast<std::string>(a)); return 0; } Question 2:
1) Is it possible to prohibit casting in C-style, so that the f(std::string(a)) or f((std::string)a) f(static_cast<std::string>(a)) compiler and f(static_cast<std::string>(a)) worked?
2) How to "teach" to trigger the call f(boost::lexical_cast<std::string>(a)) ?
(T)var, notT(var). The second is called function style cast . According to claim 1. I think it will not work out because in both cases, your transform operator is called. Those. in context, they are equivalent. If onlystatic_castin any macro not to wrap. But this is so, the thought is half awake. - 伪位蔚蠂慰位蠀蟿operator std::stringjust make thetoStringmethod? - yrHeTaTeJlb