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
}
alfC
  • 14,261
  • 4
  • 67
  • 118
  • 8
    ...and that's how template meta meta programming started class. Any questions? – Jerry Coffin Mar 25 '16 at 20:27
  • 3
    @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. http://stackoverflow.com/a/35988594/225186. Currently I have to hard code the max recursion limit. – alfC Mar 25 '16 at 20:42
  • Can't you use template argument deduction to determine the size? `template struct array_size; template struct array_size> { static const size_t result = N; };`? – templatetypedef Mar 25 '16 at 20:46
  • 1
    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 http://coliru.stacked-crooked.com/a/c3c9e3ee78d9509d – T.C. Mar 25 '16 at 21:12
  • @templatetypedef, the problem with that is that it cannot be applied to derived classes. – alfC Mar 25 '16 at 21:31
  • @T.C. 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.) – alfC Mar 25 '16 at 21:31
  • @T.C. I corrected my other answer with your solution. – alfC Mar 25 '16 at 22:20
  • Where does the question mention std::array, or have this part of the question been edited out? – user877329 Jun 19 '19 at 05:16
  • @user877329, the discussion diverged into std::array because I was trying to solve this problem: http://stackoverflow.com/a/35988594/225186 . 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. – alfC Jun 19 '19 at 05:21

0 Answers0