سؤال

When I compile the following piece of code with optimizations enabled

double bitrate = 8 * m_totBytes / (Simulator::Now() - m_startTime).GetSeconds();
double instBitrate = 8 * message.GetSize() / (Simulator::Now() - m_lastRecv).GetSeconds();
normBitrate = 0.1 * instBitrate + 0.9 * m_resolution;

NS_LOG_INFO("------RECEIVED: " << message.GetSize()
         << " Bitrate = " << bitrate
         << " instBit = " << instBitrate
         << " normBit = " << normBitrate);

I get a compiler warning saying:

error: unused variable ‘bitrate’ [-Werror=unused-variable]

because the NS_LOG_INFO macro gets optimized out by the compiler. In order to compile I have to add a useless and ugly bit of code like the following:

if (false) { // For suppressing "Unused variable" warning when compiling with "-d optimiized"
    std::cout << bitrate << instBitrate << normBitrate << std::endl;
}

How can I compile it without disabling the warnings, the optimization, and no junk code?

هل كانت مفيدة؟

المحلول

Since you're not using that variable except for in that instance, why not just do:

NS_LOG_INFO("------RECEIVED: " << message.GetSize()
     << " Bitrate = " << (8 * m_totBytes / (Simulator::Now() - m_startTime).GetSeconds())
     << " instBit = " << instBitrate
     << " normBit = " << normBitrate);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top