Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaarni/5a2da9760e100245a259 to your computer and use it in GitHub Desktop.
Save tsaarni/5a2da9760e100245a259 to your computer and use it in GitHub Desktop.
Unintentional typecast that causes infinite recursive loop
// clang++ -Wall -std=c++11 -O0 -o test test.cpp && ./test
// g++ -Wall -std=c++11 -O0 -o test test.cpp && ./test
#include <iostream>
#include <vector>
#include <cstdio>
namespace Ns
{
struct Foo;
std::ostream& operator<<(std::ostream& os, const Foo& x) { return os; }
struct Foo
{
// TO PREVENT accidental type conversion add "explicit"
// - for consideration: all non-explicit constructors with single parameter may trigger this problem
Foo(std::vector<int> i)
{
printf("HEHEE\n");
std::cout << i;
}
};
}
int main(int, char*[])
{
Ns::Foo obj{{}};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment