Access maximum template depth at compile time?
Asked Answered
A

0

6

In a certain compilation I need to play around with the option -ftemplate-depth=N that specifies the maximum template recursion.

Is it possible to access the value of the maximum template depth from the program?

I am interested in gcc or clang.

$ c++ -ftemplate-depth=128 main.cpp

#include<iostream>
int main(){
  std::cout << MAX_TEMPLATE_RECURSION << std::endl; // hypothetical name
}
Ancylostomiasis answered 25/3, 2016 at 20:22 Comment(9)
...and that's how template meta meta programming started class. Any questions?Parallelogram
@JerryCoffin, Correct! In case your are curious, this is to hack around a problem in STL, that is that std::array doesn't have a static size() function. https://mcmap.net/q/398471/-why-isn-39-t-std-array-size-static. Currently I have to hard code the max recursion limit.Ancylostomiasis
Can't you use template argument deduction to determine the size? template <typename> struct array_size; template <typename T, size_t N> struct array_size<std::array<T, N>> { static const size_t result = N; };?Wilscam
Sounds like an XY problem. You don't need "try every possible size" to handle classes derived from std::array, unless perhaps you want to allow an inaccessible/ambiguous base. See coliru.stacked-crooked.com/a/c3c9e3ee78d9509dAnking
@templatetypedef, the problem with that is that it cannot be applied to derived classes.Ancylostomiasis
@Anking Thank you, I think you made my other answer obsolete. (I suspected that it would be a XY problem, but I was curious about the max depth anyway.)Ancylostomiasis
@Anking I corrected my other answer with your solution.Ancylostomiasis
Where does the question mention std::array, or have this part of the question been edited out?Swagsman
@user877329, the discussion diverged into std::array because I was trying to solve this problem: https://mcmap.net/q/398471/-why-isn-39-t-std-array-size-static . It turns out that this was not needed to solve that problem, but technically the question doesn't have an answer and probably it doesn't have a standard answer.Ancylostomiasis

© 2022 - 2024 — McMap. All rights reserved.