C ++ line feed in varargs

#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); } 

when calling:

 LOG("src %s dst %s" ,STRIP(src_ip_)); 

I get a compilation error:

 cannot pass objects of non-trivially-copyable type 'std::string {aka struct std::basic_string<char>}' through '...' 

I understand that varargs is compatible with c, so I cannot send it a string. Is there an easy way around it? Is this correct to fix:

 #define STRIP(netIp) GeneralUtils::inet_ntop_(netIp).data() 
+4
source share
2 answers
 #define STRIP(netIp) GeneralUtils::inet_ntop_(netIp).data() 

incorrectly , it will cause undefined behavior, since it does not include trailing zero. Use

 #define STRIP(netIp) GeneralUtils::inet_ntop_(netIp).c_str() 

instead.

+4
source

You can pass const char * instead of std::string . You can take it from std::string by calling c_str()

+5
source

Source: https://habr.com/ru/post/1435325/


All Articles