Thread overview | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 30, 2012 problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Some time ago I decided to write in D something like boost :: bind.But I encountered a problem that the compiler can not deduce the template function. Here are a significant part of the code: template Combination(alias indeces,U...) { // static if(is(typeof(indeces) : uint[])) // { static if(indeces.length > 1) alias TypeTuple!(U[indeces[0]],Combination!(indeces[1..$],U)) Combination; else static if(indeces.length == 1) alias U[indeces[0]] Combination; // } } template bind(alias indeces) { // static if(is(typeof(indeces) : uint[])) // { auto bind(R,U...)(R delegate(U) dg,Combination!(indeces,U) values) { /* static if(indeces.length > 1) return bind!(update!(indeces,indeces[0]))(Curry!(indeces[0])(dg,values[0]),values[1..$]); else static if(indeces.length == 1) return Curry!(indeces[0])(dg,values[0]); */ } // } } void main() { void xyz(int x,int y,int z) { writeln("x = ",x," y = ",y," z = ",z); } // auto g = Curry!1(&xyz,cast(int)(1.1)); // g(2,3); alias Combination!([0,1],int,int,int) Arg; Arg a; bind!([0,1])(&xyz,a); readln(); } Please, explain to me what is wrong with this code |
May 30, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | I don't see anything wrong per se. It's mainly that the compiler type extraction/deduction is not powerful enough to extract U in your case. You can help it a bit by using std.traits.ParameterTypeTuple: module test; import std.stdio; import std.traits; import std.typetuple; template Combination(alias indices,U...) { static if(is(typeof(indices) : int[])) { static if(indices.length > 1) alias TypeTuple!(U[indices[0]],Combination!(indices[1..$],U)) Combination; else static if(indices.length == 1) alias U[indices[0]] Combination; } } template bind(alias indices) { static if(is(typeof(indices) : int[])) { auto bind(D,V...)(D dg, V values) if (is (D d == R delegate(U), R, U...) && is(V == Combination!(indices, ParameterTypeTuple!D))) { /* static if(indices.length > 1) return bind!(update!(indices,indices[0]))(Curry!(indices[0])(dg,values[0]),values[1..$]); else static if(indices.length == 1) return Curry!(indices[0])(dg,values[0]); */ } } } void main() { void xyz(int x,int y,int z) { writeln("x = ",x," y = ",y," z = ",z); } // auto g = Curry!1(&xyz,cast(int)(1.1)); // g(2,3); alias Combination!([0,1],int,int,int) Arg; Arg a; writeln(Arg.stringof); bind!([0,1])(&xyz,a); //readln(); } |
May 31, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | On Wednesday, 30 May 2012 at 22:19:53 UTC, Philippe Sigaud wrote:
> I don't see anything wrong per se. It's mainly that the compiler type
> extraction/deduction is not powerful enough to extract U in your case.
> You can help it a bit by using std.traits.ParameterTypeTuple:
>
> module test;
>
> import std.stdio;
> import std.traits;
> import std.typetuple;
>
> template Combination(alias indices,U...)
> {
> static if(is(typeof(indices) : int[]))
> {
> static if(indices.length > 1)
> alias
> TypeTuple!(U[indices[0]],Combination!(indices[1..$],U)) Combination;
> else static if(indices.length == 1)
> alias U[indices[0]] Combination;
> }
> }
>
> template bind(alias indices)
> {
> static if(is(typeof(indices) : int[]))
> {
> auto bind(D,V...)(D dg, V values) if (is (D d == R
> delegate(U), R, U...) && is(V == Combination!(indices,
> ParameterTypeTuple!D)))
> {
> /* static if(indices.length > 1)
> return
> bind!(update!(indices,indices[0]))(Curry!(indices[0])(dg,values[0]),values[1..$]);
> else static if(indices.length == 1)
> return Curry!(indices[0])(dg,values[0]);
> */
>
> }
> }
> }
>
> void main()
> {
> void xyz(int x,int y,int z)
> {
> writeln("x = ",x," y = ",y," z = ",z);
> }
> // auto g = Curry!1(&xyz,cast(int)(1.1));
> // g(2,3);
> alias Combination!([0,1],int,int,int) Arg;
> Arg a;
> writeln(Arg.stringof);
> bind!([0,1])(&xyz,a);
> //readln();
> }
Thank you very much:)
|
May 31, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | And, one more thing if we have the following code: template foo (T) { auto foo (U) (U arg) { } } ... foo!(int)('!'); determine whether the compiler that this specialization is to an external template? |
May 31, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Thu, May 31, 2012 at 8:31 AM, Zhenya <zheny@list.ru> wrote:
> And, one more thing
> if we have the following code:
> template foo (T)
> {
> auto foo (U) (U arg)
> {
> }
> }
> ...
> foo!(int)('!');
> determine whether the compiler that this specialization is to an external
> template?
I'm not sure I understand the question?
|
May 31, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On 31.05.2012 10:31, Zhenya wrote: > And, one more thing > if we have the following code: > template foo (T) > { > auto foo (U) (U arg) > { > } > } > ... > foo!(int)('!'); > determine whether the compiler that this specialization is to an > external template? > If you happen to be Russian, feel free to include Russian as well. I might help translate it ;) As is your question is ... hardly understandable. -- Dmitry Olshansky |
June 01, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | Вот у нас есть такая конструкция template foo(T) { foo(U)() { } } Могут ли у компилятора при инстанциировании возникнуть трудности с пониманием какие аргументы к какому шаблону относятся? Вопрос у меня возник из-за того,что когда я применил совет Philippe Sigaud к своему полному коду компилятор не захотел его компилировать.Но когда я поменял имя внутреннего шаблона все стало хорошо.Это ведь неправильно? |
June 01, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dmitry Olshansky | Вот у нас есть такая конструкция template foo(T) { foo(U)() { } } Могут ли у компилятора при инстанциировании возникнуть трудности с пониманием какие аргументы к какому шаблону относятся? Вопрос у меня возник из-за того,что когда я применил совет Philippe Sigaud к своему полному коду компилятор не захотел его компилировать.Но когда я поменял имя внутреннего шаблона все стало хорошо.Это ведь неправильно? |
June 01, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | Простите меня за мой английский) |
June 01, 2012 Re: problem with template arguments deduction | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zhenya | On Friday, 1 June 2012 at 08:08:19 UTC, Zhenya wrote: > Вот у нас есть такая конструкция > template foo(T) > { > foo(U)() > { > } > } > Могут ли у компилятора при > инстанциировании возникнуть > трудности с пониманием > какие аргументы к какому шаблону > относятся? > Вопрос у меня возник из-за того,что > когда я применил совет Philippe Sigaud > к своему полному коду компилятор не > захотел его компилировать.Но когда > я поменял имя внутреннего шаблона > все стало хорошо.Это ведь > неправильно? In other words: Given this construct: > template foo(T) > { > foo(U)() > { > } > } Could there be some problems with compiler not understanding which template argument goes to which template here? The question was born when I started applying Philippe Siguad's advice to my full source code compiler rejected it. But when I change inner tmeplate name it worked - it's no good, is it? Вопрос довольно сложный. Краткий ответ - нет, все идет слева направо, сверху вниз. Есть впрочем одно но - компилятор начинает выводить типы из аргументов только для последнего foo (т.е. U). То есть T в это случае дается сверху (тобой через foo!T(...) ). Впрочем в твоем примере foo не моежт вывести U из аргументов (их нет). Difficult Q. In short - no, it's all left-to-right up-to-bottom. However compiler may deduce types only for lowest foo (i.e. U). T in this case have to provided from above (by you as in foo!T(...)). --- It's me, just trying out web interface. |
Copyright © 1999-2021 by the D Language Foundation