1

Is it possible to store states in C++ metaprograms? I'm not entirely sure what I mean, but I want a template class that can be "assigned" to, e.g. state::value can be changed via the use of C++ templates.

I know I probably sound very vague, but I hope someone understands what I'm talking about (also please no Boost.MPL).

  • 6
    [What for?](http://www.catb.org/esr/faqs/smart-questions.html#goal) – GManNickG May 24 '11 at 05:21
  • Just interested; C++ metaprogramming looks fun! –  May 24 '11 at 05:37
  • (time-travelling) duplicate of [Is stateful metaprogramming ill-formed (yet)?](https://stackoverflow.com/questions/44267673/is-stateful-metaprogramming-ill-formed-yet) spoiler: technically no, but it probably will be eventually, and when it does, that may well be retroactive (i.e. more time travel) – underscore_d May 30 '17 at 19:57

2 Answers2

2

It's not possible, the TMP part of C++ is a purely functional language without the concept of side effects.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 2
    "state" can be passed as additional paramaters to the metafunction, thus carrying changes across calls. Writing a meta-monad is also a possibility – Joel Falcou May 25 '11 at 05:28
1

That would be a heaven for bugs.

It would also violate the template single-instantiation rule, and it will blow up the size of your program (not to mention confusing the heck out of you).

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 1
    +1, I'd say "confusing" is a very diplomatic way of describing a developer who is desperately looking for poison just to avoid debugging that code. – sharptooth May 24 '11 at 05:36