KEMBAR78
CPP Quiz | PDF
Updates About Questions The end
C++ Quiz
Felix Morgner
Computer Science
Hochschule f¨ur Technik Rapperswil
February 24, 2016
Updates About Questions The end
Updates About Questions The end
Updates About Questions The end
What we will see
Quirky stuff
Undefined Behavior
Unspecified things
Updates About Questions The end
Scoring
+1 per correct answer
+1 per correct reason
-1 per wrong answers
Updates About Questions The end
#include <iostream>
struct S {
int memfun() { return 1; }
int memfun() && { return 2; }
};
int main() {
S o{};
std::cout << o.memfun();
std::cout << S{}.memfun();
}
Updates About Questions The end
#include <iostream>
struct B {
virtual ~B() = default;
float a;
float b;
};
struct D : B {
float c;
};
int main() {
// assume: sizeof(float) = 4
// assume: sizeof(void*) = 8
std::cout << sizeof(B) << ’n’;
std::cout << sizeof(D) << ’n’;
}
Updates About Questions The end
#include <iostream>
struct S { };
int main() {
std::cout << sizeof(S) << ’n’;
}
Updates About Questions The end
#include <iostream>
auto f(unsigned long long & n) {
(***(int * * *)n)++;
}
int main() {
int constexpr n{43};
auto p1 = &n;
auto p2 = &p1;
auto p3 = (unsigned long long) &p2;
f(p3);
std::cout << (**(int * * *)p3 == &n) << ’ ’;
std::cout << *p1 << ’ ’ << n << ’n’;
}
Updates About Questions The end
#include <iostream>
int main() {
char const c{’c’};
char * ptr1;
char const * * ptr2{&ptr1};
*ptr2 = &c;
(*ptr1)++;
std::cout << c << ’n’;
}
Updates About Questions The end
#include <iostream>
int main()
{
https://github.com/fmorgner/cppquiz
std::cout << "cppugzhn";
}
Updates About Questions The end
#include <iostream>
struct S {
S() { std::cout << "c"; }
~S() { std::cout << "d"; }
};
int main() {
int i{};
mark:
S s{};
while(!i++) {
goto mark;
}
}
Updates About Questions The end
#include <iomanip>
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
int i, &j = i;
[=]{
cout << std::is_same<decltype( j ), int &>()
<< std::is_same<decltype((j)), int const &>()
<< std::is_same<int &, int const &>();
}();
}
Updates About Questions The end
#include <iostream>
struct A {
A() = default;
A(A const &) { std::cout << "An"; }
};
struct B {
B() = default;
B(B const &) { std::cout << "Bn"; }
};
auto f(B const &) { }
int main() {
A a{};
B b{};
[=]{
std::cout << sizeof(a) << ’n’;
f(b);
}();
}
Updates About Questions The end
#include <iostream>
struct E {
template<typename ...Types>
E(Types const & ...) {}
};
template<typename ...Types>
auto println(Types const & ...args) {
E{(std::cout << args)..., std::cout << ’n’};
}
int main() {
using namespace std::literals;
println("C", ’+’, "+"s, "RocksMySocks");
}
Updates About Questions The end
#include <iostream>
template<typename ...Types>
auto call(Types const & ...) { }
template<typename ...Types>
auto println(Types const & ...args) {
call((std::cout << args)..., std::cout << ’n’);
}
int main() {
using namespace std::literals;
println("C", ’+’, "+"s, "RocksMySocks");
}
Updates About Questions The end
#include <iostream>
#include <string>
#include <vector>
int main() {
auto a = "cppug";
std::vector<std::string> v{a, a, a};
for(auto const & s : v) {
std::cout << s;
}
std::cout << ’n’ << v.size() << ’n’;
}
Updates About Questions The end
#include <iostream>
#include <string>
#include <vector>
int main() {
auto a = "cppug";
std::vector<std::string> v{{a, a, a}};
for(auto const & s : v) {
std::cout << s;
}
std::cout << ’n’ << v.size() << ’n’;
}
Updates About Questions The end
#include <iostream>
#include <string>
#include <vector>
int main() {
auto a = "cppug";
std::vector<std::string> v{{a, a}};
for(auto const & s : v) {
std::cout << s;
}
std::cout << ’n’ << v.size() << ’n’;
}
Updates About Questions The end
#include <iostream>
void f(long) {
std::cout << 1 << ’n’;
}
void f(float) {
std::cout << 2 << ’n’;
}
int main() {
f(4);
}
Updates About Questions The end
#include <iostream>
void f(int, int, int, int);
void f(int, int, int, int = 1);
void f(int, int, int = 2, int);
void f(int, int = 3, int, int);
void f(int = 4, int, int, int);
void f(int a, int b, int c, int d) {
std::cout << a << b << c << d << ’n’;
}
int main() {
f();
}
Updates About Questions The end
#include <iostream>
template<typename T>
void o(T) {
std::cout << 1;
}
template<typename T>
void f(T a) {
o(a);
}
void o(float) {
std::cout << 2;
}
int main() {
f(1.0f);
}
Updates About Questions The end
#include <iostream>
int main() {
auto x = 41; // Is this the right answer ??/
x = 42;
std::cout << x;
}
Updates About Questions The end
int main() <%<:=??)??<??(&:><%??(]<%??>();%>();}();%>
Updates About Questions The end
int main() <%
<:=??)??<
??(&:><%
??(]<%??>();
%>();
}();
%>
Updates About Questions The end
int main() {
[=]{
[&]{
[]{}();
}();
}();
}
Updates About Questions The end
Thanks
https://www.youtube.com/watch?v=rNNnPrMHsAA - Piotr
Padlewski - C++ WAT
http://cppquiz.org/ - C++ Quiz
http://olvemaudal.com/ - Olve Maudal
ISO14882:2014
Updates About Questions The end
Code and Slides
https://github.com/fmorgner/cpp-quiz
http://www.slideshare.net/fmorgner/cpp-quiz

CPP Quiz

  • 1.
    Updates About QuestionsThe end C++ Quiz Felix Morgner Computer Science Hochschule f¨ur Technik Rapperswil February 24, 2016
  • 2.
  • 3.
  • 4.
    Updates About QuestionsThe end What we will see Quirky stuff Undefined Behavior Unspecified things
  • 5.
    Updates About QuestionsThe end Scoring +1 per correct answer +1 per correct reason -1 per wrong answers
  • 6.
    Updates About QuestionsThe end #include <iostream> struct S { int memfun() { return 1; } int memfun() && { return 2; } }; int main() { S o{}; std::cout << o.memfun(); std::cout << S{}.memfun(); }
  • 7.
    Updates About QuestionsThe end #include <iostream> struct B { virtual ~B() = default; float a; float b; }; struct D : B { float c; }; int main() { // assume: sizeof(float) = 4 // assume: sizeof(void*) = 8 std::cout << sizeof(B) << ’n’; std::cout << sizeof(D) << ’n’; }
  • 8.
    Updates About QuestionsThe end #include <iostream> struct S { }; int main() { std::cout << sizeof(S) << ’n’; }
  • 9.
    Updates About QuestionsThe end #include <iostream> auto f(unsigned long long & n) { (***(int * * *)n)++; } int main() { int constexpr n{43}; auto p1 = &n; auto p2 = &p1; auto p3 = (unsigned long long) &p2; f(p3); std::cout << (**(int * * *)p3 == &n) << ’ ’; std::cout << *p1 << ’ ’ << n << ’n’; }
  • 10.
    Updates About QuestionsThe end #include <iostream> int main() { char const c{’c’}; char * ptr1; char const * * ptr2{&ptr1}; *ptr2 = &c; (*ptr1)++; std::cout << c << ’n’; }
  • 11.
    Updates About QuestionsThe end #include <iostream> int main() { https://github.com/fmorgner/cppquiz std::cout << "cppugzhn"; }
  • 12.
    Updates About QuestionsThe end #include <iostream> struct S { S() { std::cout << "c"; } ~S() { std::cout << "d"; } }; int main() { int i{}; mark: S s{}; while(!i++) { goto mark; } }
  • 13.
    Updates About QuestionsThe end #include <iomanip> #include <iostream> #include <type_traits> using namespace std; int main() { int i, &j = i; [=]{ cout << std::is_same<decltype( j ), int &>() << std::is_same<decltype((j)), int const &>() << std::is_same<int &, int const &>(); }(); }
  • 14.
    Updates About QuestionsThe end #include <iostream> struct A { A() = default; A(A const &) { std::cout << "An"; } }; struct B { B() = default; B(B const &) { std::cout << "Bn"; } }; auto f(B const &) { } int main() { A a{}; B b{}; [=]{ std::cout << sizeof(a) << ’n’; f(b); }(); }
  • 15.
    Updates About QuestionsThe end #include <iostream> struct E { template<typename ...Types> E(Types const & ...) {} }; template<typename ...Types> auto println(Types const & ...args) { E{(std::cout << args)..., std::cout << ’n’}; } int main() { using namespace std::literals; println("C", ’+’, "+"s, "RocksMySocks"); }
  • 16.
    Updates About QuestionsThe end #include <iostream> template<typename ...Types> auto call(Types const & ...) { } template<typename ...Types> auto println(Types const & ...args) { call((std::cout << args)..., std::cout << ’n’); } int main() { using namespace std::literals; println("C", ’+’, "+"s, "RocksMySocks"); }
  • 17.
    Updates About QuestionsThe end #include <iostream> #include <string> #include <vector> int main() { auto a = "cppug"; std::vector<std::string> v{a, a, a}; for(auto const & s : v) { std::cout << s; } std::cout << ’n’ << v.size() << ’n’; }
  • 18.
    Updates About QuestionsThe end #include <iostream> #include <string> #include <vector> int main() { auto a = "cppug"; std::vector<std::string> v{{a, a, a}}; for(auto const & s : v) { std::cout << s; } std::cout << ’n’ << v.size() << ’n’; }
  • 19.
    Updates About QuestionsThe end #include <iostream> #include <string> #include <vector> int main() { auto a = "cppug"; std::vector<std::string> v{{a, a}}; for(auto const & s : v) { std::cout << s; } std::cout << ’n’ << v.size() << ’n’; }
  • 20.
    Updates About QuestionsThe end #include <iostream> void f(long) { std::cout << 1 << ’n’; } void f(float) { std::cout << 2 << ’n’; } int main() { f(4); }
  • 21.
    Updates About QuestionsThe end #include <iostream> void f(int, int, int, int); void f(int, int, int, int = 1); void f(int, int, int = 2, int); void f(int, int = 3, int, int); void f(int = 4, int, int, int); void f(int a, int b, int c, int d) { std::cout << a << b << c << d << ’n’; } int main() { f(); }
  • 22.
    Updates About QuestionsThe end #include <iostream> template<typename T> void o(T) { std::cout << 1; } template<typename T> void f(T a) { o(a); } void o(float) { std::cout << 2; } int main() { f(1.0f); }
  • 23.
    Updates About QuestionsThe end #include <iostream> int main() { auto x = 41; // Is this the right answer ??/ x = 42; std::cout << x; }
  • 24.
    Updates About QuestionsThe end int main() <%<:=??)??<??(&:><%??(]<%??>();%>();}();%>
  • 25.
    Updates About QuestionsThe end int main() <% <:=??)??< ??(&:><% ??(]<%??>(); %>(); }(); %>
  • 26.
    Updates About QuestionsThe end int main() { [=]{ [&]{ []{}(); }(); }(); }
  • 27.
    Updates About QuestionsThe end Thanks https://www.youtube.com/watch?v=rNNnPrMHsAA - Piotr Padlewski - C++ WAT http://cppquiz.org/ - C++ Quiz http://olvemaudal.com/ - Olve Maudal ISO14882:2014
  • 28.
    Updates About QuestionsThe end Code and Slides https://github.com/fmorgner/cpp-quiz http://www.slideshare.net/fmorgner/cpp-quiz