Topic: Template argument problem


Author: AllanW@my-dejanews.com
Date: 1998/11/24
Raw View
In article <3659358A.C1DCF3FB@victory.rosnet.ru>,
  "Andrew A. Kolessa" <andyk@victory.rosnet.ru> wrote:
>    I need to pass __FILE__ through a template argument:
> smth. like:  TClass(double, __FILE__)    cl;
>    I found example in the C++ Draft:
>
> [Example:
>           template<class T, char* p> class X {
>                   // ...
>                   X(const char* q) { /* ... */ }
>           };
>
>           // error: string literal as template-argument
>           X<int,"Studebaker"> x1;
>
>           char p[] = "Vivisectionist";
>           X<int,p> x2; // ok
>    --end example]
>
> but it seems to me that the last line is not  OK. So, how this can be
> fulfiled?

You can't use string literals in template arguments. The example
shows you an easy-to-use workaround. You can use p anywhere that
you could have used the original string literal, and you can also
use it as a template argument.

For your problem with __FILE__, you can use
    char File[] = __FILE__;
    TClass(double, File) cl;

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@eng.sun.com (Steve Clamage)
Date: 1998/11/25
Raw View
AllanW@my-dejanews.com writes:


>In article <3659358A.C1DCF3FB@victory.rosnet.ru>,
>  "Andrew A. Kolessa" <andyk@victory.rosnet.ru> wrote:
>>    I need to pass __FILE__ through a template argument:
>> smth. like:  TClass(double, __FILE__)    cl;

>You can't use string literals in template arguments. ...

>For your problem with __FILE__, you can use
>    char File[] = __FILE__;
>    TClass(double, File) cl;

Also note that "File" must have external linkage; it can't be
a local variable.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: clamage@eng.sun.com (Steve Clamage)
Date: 1998/11/25
Raw View
AllanW@my-dejanews.com writes:

>In article <3659358A.C1DCF3FB@victory.rosnet.ru>,
>  "Andrew A. Kolessa" <andyk@victory.rosnet.ru> wrote:
>>    I need to pass __FILE__ through a template argument:
>> smth. like:  TClass(double, __FILE__)    cl;

>You can't use string literals in template arguments. ...

>For your problem with __FILE__, you can use
>    char File[] = __FILE__;
>    TClass(double, File) cl;

Also note that "File" must have external linkage; it can't be
a local variable.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrew A. Kolessa" <andyk@victory.rosnet.ru>
Date: 1998/11/24
Raw View
   I need to pass __FILE__ through a template argument:
smth. like:  TClass(double, __FILE__)    cl;
   I found example in the C++ Draft:

[Example:
          template<class T, char* p> class X {
                  // ...
                  X(const char* q) { /* ... */ }
          };

          X<int,"Studebaker"> x1; // error: string literal as
template-argument

          char p[] = "Vivisectionist";
          X<int,p> x2; // ok
   --end example]

but it seems to me that the last line is not  OK. So, how this can be
fulfiled?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]