A module comprehensive template-specialization
Matthias Walter
xammy at xammy.homelinux.net
Mon Jun 28 02:09:13 PDT 2010
On 06/28/2010 09:49 AM, Justin Spahr-Summers wrote:
> On Sun, 27 Jun 2010 18:51:35 +0200, Matthias Walter
> <xammy at xammy.homelinux.net> wrote:
>>
>> Hi list,
>>
>> I tried to write a traits class comparable to iterator_traits in C++ STL
>> or graph_traits in Boost Graph Library in D 2.0, but failed to do so via
>> template specialization which is put into different modules. Putting
>> everything into one module interferes with extensibility. I tried the
>> following:
>>
>> == Module a ==
>> | module a;
>> |
>> | template Base (T)
>> | {
>> | alias T Base;
>> | }
>>
>> == Module b ==
>> | module b;
>> |
>> | import a;
>> |
>> | template Base(T: T*)
>> | {
>> | alias Base !(T) Base;
>> | }
>>
>> == Main module ==
>> |
>> | import a, b;
>> |
>> | int main(char[][] args)
>> | {
>> | alias Base !(int*) foo;
>> |
>> | return 0;
>> | }
>>
>> The error message is:
>> "bug.d(8): Error: template instance ambiguous template declaration
>> b.Base(T : T*) and a.Base(T)"
>>
>> Can I handle this in another way (like making the template a conditional
>> one)?
>>
>> best regards
>> Matthias Walter
>
> I believe this is intended behavior, as it prevents template hijacking
> and the like. Using alias to import the two templates into the same
> scope might help, though I'm not sure exactly how it should be done.
I tried to do so in some variants but did not succeed unfortunately. If
you have a precise idea, please let me know!
>
> On another note, though, have you looked at __traits() and std.traits?
I looked at them but didn't find them helpful for this precise problem.
The whole reason for doing this is to make it possible to make another
existing class model the concept (i.e. have some aliases / typedefs
done) of my library class without editing any of them. As I mentioned in
my other response, a prominent example for Boost Graph
Library is the LEDA graph class, which can be enabled to be used by BGL
by more or less just specializing the graph_traits template. I'd like to
have this kind of technique available, too.
Any further suggestions?
More information about the Digitalmars-d-learn
mailing list