c ++将字符串传递给varargs

Avi*_*ano 4 c++ string

#define LOG(format,...) Logger::Log(format,__VA_ARGS__)
#define STRIP(netIp) GeneralUtils::inet_ntop_(netIp)
string GeneralUtils::inet_ntop_(unsigned int netIp){
    char strIP[INET_ADDRSTRLEN];
    in_addr sin_addr;
    sin_addr.s_addr = netIp;
    inet_ntop(AF_INET, &sin_addr.s_addr, strIP, sizeof strIP);
    return string(strIP);
}
Run Code Online (Sandbox Code Playgroud)

致电:

LOG("src %s dst %s" ,STRIP(src_ip_));
Run Code Online (Sandbox Code Playgroud)

我得到编译错误:

cannot pass objects of non-trivially-copyable type ‘std::string {aka struct std::basic_string<char>}’ through ‘...’
Run Code Online (Sandbox Code Playgroud)

我知道varargs是c兼容的,所以我不能发送字符串.有没有一种简单的方法可以绕过它?修复它是否正确如下:

#define STRIP(netIp) GeneralUtils::inet_ntop_(netIp).data()
Run Code Online (Sandbox Code Playgroud)

nog*_*ard 5

你可以通过const char *代替std::string.你可以std::string通过电话接听它c_str()