C++ Template Metaprogramming | HFT Interview Question
Вставка
- Опубліковано 27 січ 2023
- In this video I have discussed a template metaprogramming question which is commonly asked in HFT Interviews.
Question: Write a template metafunction to find dimension of array of any type at compile time. Dimension of an array is also known as rank of array.
Implementation of std::rank in C++: en.cppreference.com/w/cpp/typ...
Connect with me on Instagram: / shubham__cr7
I really liked your explanation and even though there are some topics I still need to research in order to fully unnderstand the code, the aspect related to template metaprogramming is much more clearer now, Thanks!
great to hear!
how and from where to study oops in c++ ?
had to set playback to 0.75, jezus man slow down haha
🚀🚀🚀
@Coding Interview Prep Your solution for this problem was overcomplicated, this is a much more concise compile-time solution:
```
template
constexpr size_t dimension_count(const T&) {
return 0;
}
template
constexpr size_t dimension_count(const T(&)[I]) {
return 1 + dimension_count(T{});
}
```
your solution requires that someone passes a value. It wont compile when we will call it like dimension_count(int[][3]). there are many scenarios in which your solution wont compile.
Also you are instantiating T like T{} in second method. What if T’s default constructor is deleted or does not exist???
My solution covers all the edge cases. I put a lot of thought into each of my tutorial and try to come up with the best possible solution
and I think that I am fairly good at that. You can definitely come up with something simpler than my solution which covers all the edge cases but it won’t be easy and would require a fair amount of thought
@@CodingInterviewPrep Yes you are right, I only considered trivially_constructible types for T and didn't put an assert to guarantee it.
To check only by giving a type can only be done using struct templates as your solution does.