digitalmars.D.learn - Template string literal?
- Jacob Carlborg (13/13) Mar 16 2011 Is it possible to declare string literal of a template type, something
- Simen kjaeraas (22/33) Mar 16 2011 Not having D available, I would say this should work:
- Denis Koroskin (2/18) Mar 16 2011
- Simen kjaeraas (5/31) Mar 16 2011 I shoulda thought of that. Much cleaner than my solution.
- Jacob Carlborg (4/36) Mar 17 2011 Yeah, me too. Thanks.
Is it possible to declare string literal of a template type, something like this: void bar (const(T)[] a) {} void foo (T) (const(T)[] a) { bar("abc"T); } foo("def"w); In this case the string literal passed to "bar" would be of the type "wstring". Is this somehow possible or do I have to create a variable? -- /Jacob Carlborg
Mar 16 2011
On Wed, 16 Mar 2011 22:30:17 +0100, Jacob Carlborg <doob me.com> wrote:Is it possible to declare string literal of a template type, something like this: void bar (const(T)[] a) {} void foo (T) (const(T)[] a) { bar("abc"T); } foo("def"w); In this case the string literal passed to "bar" would be of the type "wstring". Is this somehow possible or do I have to create a variable?Not having D available, I would say this should work: void foo( T )( const( T )[] a ) { bar( cast(T)"abc" ); } Otherwise: template stringSuffix( T ) { static if ( is( Unqual!T == char ) ) { enum stringSuffix = "c"; } else static if ( is( Unqual!T == wchar ) ) { enum stringSuffix = "w"; } else static if ( is( Unqual!T == dchar ) ) { enum stringSuffix = "d"; } else { static assert( false ); } } void foo( T )( const( T )[] a ) { bar( mixin( `"abc"` ~ stringSuffix!T ) ); } -- Simen
Mar 16 2011
On Thu, 17 Mar 2011 00:30:17 +0300, Jacob Carlborg <doob me.com> wrote:Is it possible to declare string literal of a template type, something like this: void bar (const(T)[] a) {} void foo (T) (const(T)[] a) { bar("abc"T); } foo("def"w); In this case the string literal passed to "bar" would be of the type "wstring". Is this somehow possible or do I have to create a variable?void bar (const(T)[] a) {} void foo (T) (const(T)[] a) {bar!(T)("abc"); // same as: const(T)[] abc = "abc"; bar(abc);} foo("def"w);
Mar 16 2011
On Wed, 16 Mar 2011 22:39:00 +0100, Denis Koroskin <2korden gmail.com> wrote:On Thu, 17 Mar 2011 00:30:17 +0300, Jacob Carlborg <doob me.com> wrote:I shoulda thought of that. Much cleaner than my solution. -- SimenIs it possible to declare string literal of a template type, something like this: void bar (const(T)[] a) {} void foo (T) (const(T)[] a) { bar("abc"T); } foo("def"w); In this case the string literal passed to "bar" would be of the type "wstring". Is this somehow possible or do I have to create a variable?void bar (const(T)[] a) {} void foo (T) (const(T)[] a) {bar!(T)("abc"); // same as: const(T)[] abc = "abc"; bar(abc);} foo("def"w);
Mar 16 2011
On 2011-03-16 22:41, Simen kjaeraas wrote:On Wed, 16 Mar 2011 22:39:00 +0100, Denis Koroskin <2korden gmail.com> wrote:Yeah, me too. Thanks. -- /Jacob CarlborgOn Thu, 17 Mar 2011 00:30:17 +0300, Jacob Carlborg <doob me.com> wrote:I shoulda thought of that. Much cleaner than my solution.Is it possible to declare string literal of a template type, something like this: void bar (const(T)[] a) {} void foo (T) (const(T)[] a) { bar("abc"T); } foo("def"w); In this case the string literal passed to "bar" would be of the type "wstring". Is this somehow possible or do I have to create a variable?void bar (const(T)[] a) {} void foo (T) (const(T)[] a) {bar!(T)("abc"); // same as: const(T)[] abc = "abc"; bar(abc);} foo("def"w);
Mar 17 2011