0

If following is the definition of simple function template:

template<typename T>
T compare(T a, T b)
{
   return a>b ? a : b;
}

Can it be called with different template parameter based on some user input during runtime, WITHOUT creating class templates, with different T values as follows for example:

char type;
cout<<"Enter type: ";
cin>>type;

if( type=='i')
{
   int x=compare<int>(3,6);
}
else if( type=='d' )
{
   double z=compare<double>(5.1,7.9);
}
..so on
leopra
  • 17
  • 4
  • 2
    What's wrong with what you wrote as your example? – ChrisMM Dec 14 '21 at 04:39
  • Please use correct terminology: _class template_ and _function template_. – paddy Dec 14 '21 at 04:40
  • sorry, made a few typos there but now corrected them! – leopra Dec 14 '21 at 04:43
  • Hint: it's in the name -- a function template is like a blueprint for building a function. Expanding a template into code requires a compiler, and thus is a language feature, not a runtime behavior. – paddy Dec 14 '21 at 04:50
  • @paddy so this code is then incorrect? – leopra Dec 14 '21 at 04:52
  • You keep asking for validation, and I think you're not understanding what's been said. Nor are you reading the answer that's already been written. It says that the correct approach is to do what you're currently doing. I agree. Although it isn't really clear from this toy example what end result you want to achieve, nor is it clear what you consider the alternative to be. – paddy Dec 14 '21 at 04:55
  • 1
    *"WITHOUT creating class templates"* -- how would a class template help? – JaMiT Dec 14 '21 at 06:58
  • All your templates will be instantiated at compile time. You select one of the created functions at runtime later. That means, you have all your template instances in your executable even if you never use them in runtime. – Klaus Dec 14 '21 at 08:47
  • 1
    [OT]: `T` is deducible, so `int x = compare(3,6);`/`double z = compare(5.1, 7.9);` would be enough. – Jarod42 Dec 14 '21 at 08:51
  • Thank you all. @Klaus your comment that all instances are created at compile time and chosen at runtime even though some might never be used answers my main doubt. – leopra Dec 16 '21 at 04:15
  • @JaMiT I tried to find similar question and they solved the problem based on interfaces in this example: https://stackoverflow.com/questions/2873802/specify-template-parameters-at-runtime/2876096 – leopra Dec 16 '21 at 04:16
  • @leopra Wow. That becomes an astounding amount of over-engineering when applied to function templates. When you adapt an answer to a new situation, you should not assume that your approach is well-known (or even reasonable), especially if it seems awkward (or worse) to you. **This is advice for the future,** but I'll use this question as a concrete example. I would take out the *"WITHOUT creating class templates"* qualification, and instead add a paragraph *at the bottom* explaining that you found a similar question (include the link), but that adapting it looked a lot worse than you'd like.) – JaMiT Dec 17 '21 at 00:39
  • @JaMiT Thanks for the pointers. I'll keep those in mind! – leopra Dec 17 '21 at 04:01

1 Answers1

0

No. If you want to determine the type at runtime then you need to do what are you doing (the if else)

There are more advanced techniques for more advance uses, like using polymorphism with a table of callables, but in essence they do the same thing, just in a fancier way.

bolov
  • 72,283
  • 15
  • 145
  • 224