C++ template for function need be in compile time

is template for function changes syntax at compile time or no, e.g.

1
2
3
4
5
6
7
8
9
10
11
template<bool o>
test(int& pos, int depth) {
  int cnt, n = 0;
  bool a = (depth == 2);
  if (o) 
    cout << "test";
  if (o && depth <= 1)
       cout << " depth = 1"
  else
    { cout << "test for FALSE "; }
}

if no, how the true syntax to do its real metaprogramming (compile time)
Last edited on
as o is a templated param, you use a constexpr if for it to be evaluated at compile time.

Something like (not tried):
1
2
3
4
5
6
7
8
9
10
11
12
13
template<bool o>
void test(int& pos, int depth) {
	int cnt {}, n {};
	bool a { depth == 2 };

	if constexpr (o) {
		cout << "test";

		if (depth <= 1)
			cout << " depth <= 1"
	} else
		cout << "test for FALSE ";
}

Last edited on
Topic archived. No new replies allowed.