4

Lets say i have a template:

template<char const *str>
class Template { ... };

Why is it not possible to write the following?

Template<"literal"> T;

or

char const *s = "Literal";
Template<s> T;

And why does the following works?

char const s[] = "Literal";
Template<s> T;
Anon
  • 1,274
  • 4
  • 17
  • 44
  • 2
    because the standard says so. – yngccc Jan 18 '14 at 16:26
  • 4
    @yngum while that's true on a certain level, it's not very helpful as an answer. – tenfour Jan 18 '14 at 16:45
  • @tenfour See [Passing const char* as template argument](http://stackoverflow.com/a/3854954/1508519). There is a pretty good explanation for the rationale behind not allowing it, but it comes down to speculation, doesn't it? –  Jan 18 '14 at 16:46
  • @yngum, is there a good reason that the standard says so? Or, is there a good reason not to allow the examples given? – Drew Noakes Jan 18 '14 at 16:47
  • 1
    It would be wonderful if someone explained the logic behind this language constraint! :) – Anon Jan 18 '14 at 18:58

1 Answers1

-1

Because template classes are based on types and not on values.
templates are meant to help you write a more generic code, one that is the same for several types of data so you don't have to write it for each type all over again.

Idov
  • 5,006
  • 17
  • 69
  • 106
  • 1
    Templates can have type and non-type parameters. For example `template struct foo { double arr[N]; }; foo<3> my_foo;` is perfectly fine. – dyp Jan 19 '14 at 01:39