A
Ares Lagae
I have a strange problem with the code below. I _suspect_ a compiler
bug. When compiling the code below with 7.1, i get a number of
"undefined variable" errors, but the code should compile fine. However
1) If I remove the forward declaration
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>&, const
timer&);
the same code compiles fine
2) If I change to forward declaration to include parameter names
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>& os,
const timer& t);
the same code compiles fine
3) If I remove the stringstream part
replace
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::now()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceCounter() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
in both timer::now() and timer::frequency()
by
if (bResult == FALSE)
{
throw std::runtime_error("");
}
the same code compiles fine.
Does anyone know what's going on here ?
Best regards,
Ares Lagae
------------------------------ BEGIN CODE ------------------------------
// declaration
#include <ostream>
class timer
{
public:
timer();
void start();
void stop();
long long elapsed() const;
long long frequency() const;
private:
long long now() const;
long long start_;
long long stop_;
bool running_;
};
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>&, const
timer&);
// implementation
#include <stdexcept>
#include <sstream>
#include <cassert>
#include <windows.h>
timer::timer()
: start_(0), stop_(0), running_(false)
{
}
long long timer::now() const
{
LARGE_INTEGER liPerformanceCount;
BOOL bResult = QueryPerformanceCounter(&liPerformanceCount);
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::now()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceCounter() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
return liPerformanceCount.QuadPart;
}
long long timer::frequency() const
{
LARGE_INTEGER liFrequency;
BOOL bResult = ::QueryPerformanceFrequency(&liFrequency);
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::frequency()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceFrequency() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
return liFrequency.QuadPart;
}
void timer::start()
{
assert(running_ == false);
running_ = true;
start_ = now();
}
void timer::stop()
{
assert(running_ == true);
stop_ = now();
running_ = false;
}
long long timer::elapsed() const
{
return running_ ? now() - start_ : stop_ - start_;
}
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>& os,
const timer& t)
{
return os << t.elapsed() << ' ' << '(' << t.frequency() << ')'
<< std::endl;
}
// program
#include <iostream>
int main(int argc, char* argv)
{
try
{
timer t;
t.start();
t.stop();
std::cout << t << std::endl;
}
catch (std::runtime_error & e)
{
std::cout << e.what() << std::endl;
}
}
bug. When compiling the code below with 7.1, i get a number of
"undefined variable" errors, but the code should compile fine. However
1) If I remove the forward declaration
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>&, const
timer&);
the same code compiles fine
2) If I change to forward declaration to include parameter names
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>& os,
const timer& t);
the same code compiles fine
3) If I remove the stringstream part
replace
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::now()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceCounter() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
in both timer::now() and timer::frequency()
by
if (bResult == FALSE)
{
throw std::runtime_error("");
}
the same code compiles fine.
Does anyone know what's going on here ?
Best regards,
Ares Lagae
------------------------------ BEGIN CODE ------------------------------
// declaration
#include <ostream>
class timer
{
public:
timer();
void start();
void stop();
long long elapsed() const;
long long frequency() const;
private:
long long now() const;
long long start_;
long long stop_;
bool running_;
};
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>&, const
timer&);
// implementation
#include <stdexcept>
#include <sstream>
#include <cassert>
#include <windows.h>
timer::timer()
: start_(0), stop_(0), running_(false)
{
}
long long timer::now() const
{
LARGE_INTEGER liPerformanceCount;
BOOL bResult = QueryPerformanceCounter(&liPerformanceCount);
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::now()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceCounter() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
return liPerformanceCount.QuadPart;
}
long long timer::frequency() const
{
LARGE_INTEGER liFrequency;
BOOL bResult = ::QueryPerformanceFrequency(&liFrequency);
if (bResult == FALSE)
{
std::stringstream error_msg;
error_msg << "timer::frequency()"
<< ' ' << ':' << ' ' <<
"QueryPerformanceFrequency() failed"
<< ' ' << '(' << "GetLastError() = " <<
::GetLastError() << ')'
<< ' ' << '(' << __FILE__ << '(' << __LINE__ <<
')' << ')';
throw std::runtime_error(error_msg.str());
}
return liFrequency.QuadPart;
}
void timer::start()
{
assert(running_ == false);
running_ = true;
start_ = now();
}
void timer::stop()
{
assert(running_ == true);
stop_ = now();
running_ = false;
}
long long timer::elapsed() const
{
return running_ ? now() - start_ : stop_ - start_;
}
template <typename Ch, typename Tr>
std::basic_ostream<Ch,Tr>& operator<<(std::basic_ostream<Ch,Tr>& os,
const timer& t)
{
return os << t.elapsed() << ' ' << '(' << t.frequency() << ')'
<< std::endl;
}
// program
#include <iostream>
int main(int argc, char* argv)
{
try
{
timer t;
t.start();
t.stop();
std::cout << t << std::endl;
}
catch (std::runtime_error & e)
{
std::cout << e.what() << std::endl;
}
}