Skip to content

Instantly share code, notes, and snippets.

@dabrahams
Created December 30, 2011 16:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabrahams/1540564 to your computer and use it in GitHub Desktop.
Save dabrahams/1540564 to your computer and use it in GitHub Desktop.
Test of where noexcept-ness is measured
#include <iostream>
#include <exception>
#include <cassert>
struct tracked
{
int id;
static int next;
tracked()
: id(next++)
{
std::cout << "ctor of object " << id << std::endl;
}
tracked(tracked&& x)
{
std::cout << "throwing move of object " << x.id << std::endl;
throw "throwing move";
}
tracked(tracked const&) = delete;
~tracked()
{
std::cout << "dtor of object " << id << std::endl;
}
};
int tracked::next = 0;
struct scope
{
scope() { std::cout << "++scope" << std::endl; }
~scope() { std::cout << "--scope" << std::endl; }
};
void argument_test(tracked) noexcept
{
// should never get here
scope s;
std::cout << "in argument_test" << std::endl;
}
tracked return_test() noexcept
{
scope s;
std::cout << "in return_test" << std::endl;
tracked t;
return std::move(t); // ensure there's no RVO
}
void my_terminate()
{
std::cout << "return values are moved in the callee" << std::endl;
abort();
}
int main()
{
try
{
tracked t;
std::cout << "about to pass throwing argument to noexcept function" << std::endl;
argument_test(std::move(t));
}
catch(...)
{
std::cout << "caught" << std::endl;
std::cout << "arguments are moved in the caller" << std::endl;
}
std::cout << "---------------" << std::endl;
try
{
std::cout << "about to return throwing result from noexcept function" << std::endl;
std::set_terminate(my_terminate);
return_test();
}
catch(...)
{
std::cout << "return values are moved in the caller" << std::endl;
std::cout << "caught" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment