2

I want to input the parameter (int) from keyboard and then pass it to template for creating an object. The template is

int lim;

cin>>lim;

mpa<lim,int,const char*> C;`

But it is required a constant expression. How to realize this action?

user1290126
  • 43
  • 1
  • 1
  • 6
  • 1
    Templates are compile-time constructs. They don't exist at runtime. I/O happens a runtime. It can't be done at compile-time. From this follows that what you seek is not feasible. Maybe if you explain what your goal is someone can provide an alternative. – R. Martinho Fernandes Mar 27 '12 at 16:03
  • Template parameters have to be compile time constants. This is required because the compiler has to generate the template code at compile time. – Chad Mar 27 '12 at 16:03

1 Answers1

4

It is impossible. Templates are instantiated during compile-time, and you want to change behavior in run-time. Alternatively if set of possible parameter values are know, you can mention them in switch / case or similar construct. Of course when using this technique one should be aware of code bloat - all instantiations of template will be compiled into binary, so this is not good way to do things at all

Alex Z
  • 1,867
  • 1
  • 29
  • 27
  • the switch/case addition ruins a perfectly good answer - you should delete it. +1 all the same, for the clear distinction between run-time and compile time. – amit Mar 27 '12 at 16:05
  • 1
    why? If the user input falls within a known range, then he could use this paradigm, provided that the performance gains of employing templates is significant enough to offset the ugliness of the approach. – akappa Mar 27 '12 at 16:06
  • @akappa: In this case - the [abstract factory pattern](http://en.wikipedia.org/wiki/Abstract_factory_pattern) is the right choice, and not a switch and series of case statements. It is both more readable, more maintainable and in most cases - more efficient as well. – amit Mar 27 '12 at 16:08
  • Frankly I can't think of any such case myself :) – Alex Z Mar 27 '12 at 16:08
  • @Amit if you want to instantiate different classes you will have that ugly switch inside the Factory anyway – Alex Z Mar 27 '12 at 16:09
  • @amit: and what would be the return type of the method? Because, if you have a set of static `newX()` member fields in your factory, each one with the proper return type, then you need a `switch` to correctly invoke the factory. If, instead, you provide a `generic_mpa` which can be derived by `mpa` and returned by `new(int)`, then the factory must employ a switch. To wrap up: he needs anyway a switch, but you can mask it engineering a bit the code. – akappa Mar 27 '12 at 16:13
  • 1
    @Fahrenheit2539: No, you won't - you will have populate a `map` [only once in the program life time] and invoke `map[lim].build()` Each of `AbstractFactory` implementing classes will have an overriding method `build()` which will return a superclass of `mpa`. The implementing method will return the specific `mpa` object – amit Mar 27 '12 at 16:15
  • @amit: but the code that populates the map isn't less ugly, and you need to bloat further the code by providing a templatized `AbstractFactory` on the integer. – akappa Mar 27 '12 at 16:17
  • @akappa: I disagree - the initialization is done only once in the program life-time. I believe most programmers will agree that using the abstract factory pattern here is more elegant, but I could be wrong. – amit Mar 27 '12 at 16:20
  • @amit: perhaps my tastes on programming aren't well represented in the community, but I rate simplicity and efficiency more than "elegance". Having just one factory with a `switch` inside it looks more comprehensible on a first sight rather than two factories (a front-end and a templatized one), a map and a code which fills it. – akappa Mar 27 '12 at 16:24
  • 1
    @akappa: I think it is one of the places we can simply agree to disagree :) – amit Mar 27 '12 at 16:25
  • @amit: sure, as for anything that boils down to tastes :) – akappa Mar 27 '12 at 16:29