Domanda di colloquio di Bloomberg

C++ template metaprogramming (that team was into functional programming and compile time computations). Standard, find nth Fibonacci number using recursive templates.

Risposte di colloquio

Anonimo

30 ott 2014

The obvious simple solution: #include template struct FIB { enum { RESULT = FIB::RESULT + FIB::RESULT }; }; template struct FIB { enum { RESULT = 0 }; }; template struct FIB { enum { RESULT = 1 }; }; int main() { std::cout ::RESULT << std::endl; return EXIT_SUCCESS; }

Anonimo

15 apr 2019

template struct fibonacci { using type = unsigned long int; static constexpr type value = fibonacci::value + fibonacci::value; }; template struct fibonacci { using type = unsigned long int; static constexpr type value = 0; }; template struct fibonacci { using type = unsigned long int; static constexpr type value = 1; }; int main() { return fibonacci::value; }