Why isn't a for-loop a compile-time expression?

后端 未结 4 1907
自闭症患者
自闭症患者 2020-11-30 09:32

If I want to do something like iterate over a tuple, I have to resort to crazy template metaprogramming and template helper specializations. For example, the following progr

4条回答
  •  天命终不由人
    2020-11-30 10:13

    Why isn't a for-loop a compile-time expression?

    Because a for() loop is used to define runtime control flow in the c++ language.

    Generally variadic templates cannot be unpacked within runtime control flow statements in c++.

     std::get(t);
    

    cannot be deduced at compile time, since i is a runtime variable.

    Use variadic template parameter unpacking instead.


    You might also find this post useful (if this not even remarks a duplicate having answers for your question):

    iterate over tuple

提交回复
热议问题