Skip to content

Instantly share code, notes, and snippets.

@mosra
Created May 25, 2015 08:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosra/7363ac0f226f6370c78d to your computer and use it in GitHub Desktop.
Save mosra/7363ac0f226f6370c78d to your computer and use it in GitHub Desktop.
Voldemort types in C++14

Inspired with real D language feature: http://wiki.dlang.org/Voldemort_types

Compile in GCC/Clang with -std=c++14.

#include <iostream>
#include <typeinfo>

auto eh() {
	struct Voldemort {
		int b;
	};
	
	return Voldemort{3};
}

int main() {
	//eh()::Voldemort c = eh(); // no, error
	//eh::Voldemort c = eh();   // nope
	//Voldemort c = eh();       // also nope
	auto c = eh();              // works!

	// demangled type says eh()::Voldemort but cannot be used
	std::cout << "type: " << typeid(c).name() << std::endl;
	std::cout << "value: " << c.b << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment