You could write something like the following:
template<typename T>
void alert(T item)
{
MessageBox(NULL, boost::lexical_cast<std::string>(item).c_str(), "Message", MB_OK | MB_ICONINFORMATION);
}
You should specialize boost::lexical_cast
for any argument type you want since it supports limited range of types.
Another way is to use boost::format
:
// helper class
class msg_helper_t {
public:
msg_helper_t(const char* msg ) : fmt(msg) {}
~msg_helper_t() {
MessageBox(NULL, str( fmt ).c_str(), "Message", MB_OK | MB_ICONINFORMATION);
}
template <typename T>
msg_helper_t& operator %(const T& value) {
try {
fmt % value;
} catch ( std::exception e ) {
// some exceptional actions
}
return *this;
}
template <>
msg_helper_t& operator %(const CString& value) {
try {
fmt % value.GetString();
} catch ( std::exception e ) {
// some exceptional actions
}
return *this;
}
protected:
boost::format fmt;
};
// our message function
msg_helper_t MyMsgBox(const char* msg) { return msg_helper_t( msg ); }
Later it could be used in the following way:
MyMsgBox( "Hello with %d arguments: %s" ) % 2 % "some text";
MyMsgBox( "%d" ) % 123456;
MyMsgBox( "%f" ) % 10.5f;