 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Kai Ruhnau Guest
|
Posted: Mon Jun 21, 2004 10:33 pm Post subject: Template Argument Deduction |
|
|
Hi NG,
Gegeben sei folgender Code:
template <int N>
void f(int foo[N][N], int bar[N])
{
}
template <int N>
void g(int foo[N*N], int bar[N])
{
}
template <int N>
void h(int bar[N], int foo[N*N])
{
}
template <int N, int M>
void i(int foo[N], int bar[M])
{
}
int main()
{
int foo1[2][2]={{1,2},{3,4}};
int foo2[4]={1,2,3,4};
int bar[2]={2,1};
f(foo1,bar);
g(foo2,bar);
h(bar,foo2);
i(foo2,bar);
}
Warum kann f instanziert werden (ok, den Teil dachte ich zu verstehen),
g, h und i aber nicht?
Kann der Compiler nicht aus dem Argument bar ableiten, welche Größe N
haben muss? Bzw. wo ist bei i der Unterschied zu f?
Sehr recht wäre mir neben einer schlüssigen Erklärung auch eine Stelle
im Standard, wo ich darüber lesen kann.
Grübelnd und Grüße
Kai
--
This signature is left as an exercise for the reader.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Mon Jun 21, 2004 10:58 pm Post subject: Re: Template Argument Deduction |
|
|
Kai Ruhnau wrote:
| Quote: | Hi NG,
Gegeben sei folgender Code:
template <int N
void f(int foo[N][N], int bar[N])
{
}
template
void g(int foo[N*N], int bar[N])
{
}
template
void h(int bar[N], int foo[N*N])
{
}
template
void i(int foo[N], int bar[M])
{
}
int main()
{
int foo1[2][2]={{1,2},{3,4}};
int foo2[4]={1,2,3,4};
int bar[2]={2,1};
f(foo1,bar);
g(foo2,bar);
h(bar,foo2);
i(foo2,bar);
}
Warum kann f instanziert werden (ok, den Teil dachte ich zu verstehen),
g, h und i aber nicht?
Kann der Compiler nicht aus dem Argument bar ableiten, welche Größe N
haben muss? Bzw. wo ist bei i der Unterschied zu f?
|
Wie wäre es mit Fehlermeldungen von Deinenm Compiler? Zu g() und h(),
welche Funktionen neben Quadraten hättest Du gerne von Deinen Compiler
zur compilezeit geprüft? void w(int[N^2], int[n]) oder
g(int[N*42*sqrt(42)],int[n])? Allein um zu überprüfen, ob ein
Funktionsaufruf zu
template
void g(int foo[N*N], int bar[N])
past, müste der compiler die Wurzel aus N*N ziehen und gucken, ob ein
natürliches N bei raus kommen würde.
Torsten
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Kai Ruhnau Guest
|
Posted: Mon Jun 21, 2004 11:39 pm Post subject: Re: Template Argument Deduction |
|
|
Torsten Robitzki wrote:
| Quote: | Kai Ruhnau wrote:
Hi NG,
Gegeben sei folgender Code:
template
void f(int foo[N][N], int bar[N])
{
}
template
void g(int foo[N*N], int bar[N])
{
}
template
void h(int bar[N], int foo[N*N])
{
}
template
void i(int foo[N], int bar[M])
{
}
int main()
{
int foo1[2][2]={{1,2},{3,4}};
int foo2[4]={1,2,3,4};
int bar[2]={2,1};
f(foo1,bar);
g(foo2,bar);
h(bar,foo2);
i(foo2,bar);
}
Warum kann f instanziert werden (ok, den Teil dachte ich zu
verstehen), g, h und i aber nicht?
Kann der Compiler nicht aus dem Argument bar ableiten, welche Größe N
haben muss? Bzw. wo ist bei i der Unterschied zu f?
Wie wäre es mit Fehlermeldungen von Deinenm Compiler?
|
'Tschuldigung:
gcc:
deduction-test.cpp:29: error: no matching function for call to
`g(int[4],int[2])'
deduction-test.cpp:30: error: no matching function for call to
`h(int[2],int[4])'
deduction-test.cpp:31: error: no matching function for call to
`i(int[4],int[2])'
Comeau Online:
"ComeauTest.c", line 29: error: no instance of function template "g"
matches the
argument list
The argument types that you used are: (int [4], int [2])
g(foo2,bar);
^
"ComeauTest.c", line 30: error: no instance of function template "h"
matches the
argument list
The argument types that you used are: (int [2], int [4])
h(bar,foo2);
^
"ComeauTest.c", line 31: error: no instance of function template "i"
matches the
argument list
The argument types that you used are: (int [4], int [2])
i(foo2,bar);
^
VC++ 7.1:
rumtestprojekt.cpp(2 : error C2784: 'void f(int [N][N],int [N])':
Vorlagenargument für 'int [N][N]' von 'int [2][2]' konnte nicht
hergeleitet werden
rumtestprojekt.cpp(29): error C2784: 'void g(int [N*N],int [N])':
Vorlagenargument für 'int [N*N]' von 'int [4]' konnte nicht hergeleitet
werden
rumtestprojekt.cpp(30): error C2784: 'void h(int [N],int [N*N])':
Vorlagenargument für 'int [N]' von 'int [2]' konnte nicht hergeleitet werden
rumtestprojekt.cpp(31): error C2784: 'void i(int [N],int [M])':
Vorlagenargument für 'int [N]' von 'int [4]' konnte nicht hergeleitet werden
| Quote: | Zu g() und h(),
welche Funktionen neben Quadraten hättest Du gerne von Deinen Compiler
zur compilezeit geprüft?
|
Nur diese, es geht darum ein Matrix und einen Vektor an ein Template zu
übergeben. (Ich habe es schließlich mit f() gelöst und meine Matrix
entsprechend deklariert).
| Quote: | void w(int[N^2], int[n]) oder
g(int[N*42*sqrt(42)],int[n])? Allein um zu überprüfen, ob ein
Funktionsaufruf zu
template
void g(int foo[N*N], int bar[N])
past, müste der compiler die Wurzel aus N*N ziehen und gucken, ob ein
natürliches N bei raus kommen würde.
|
Oder er müsste bei bar nachschauen und einfach N*N rechnen. Das
irritiert mich ja gerade. Bzw. was läuft da bei i() falsch?
Danke und Grüße
Kai
--
This signature is left as an exercise for the reader.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Torsten Robitzki Guest
|
Posted: Tue Jun 22, 2004 5:28 pm Post subject: Re: Template Argument Deduction |
|
|
Hallo Kai,
| Quote: |
Oder er müsste bei bar nachschauen und einfach N*N rechnen. Das
irritiert mich ja gerade. Bzw. was läuft da bei i() falsch?
|
Jetzt fällt mir wieder ein, das ich eine ähnliche Frage vor Jahren schon
mal gestellt habe und Markus Schaaf war damals so nett, mir die Lösung
zu verraten: "Das Array muß als Referenz übergeben werden".
template <int N>
void f(int (&foo)[N][N], int (&bar)[N])
{
}
template <int N>
void g(int (&foo)[N*N], int (&bar)[N])
{
}
template <int N>
void h(int (&bar)[N], int (&foo)[N*N])
{
}
template <int N, int M>
void i(int (&foo)[N], int (&bar)[M])
{
}
int main()
{
int foo1[2][2]={{1,2},{3,4}};
int foo2[4]={1,2,3,4};
int bar[2]={2,1};
f(foo1,bar);
g(foo2,bar);
h(bar,foo2);
i(foo2,bar);
}
läßt sich mit g++ problemlos übersetzen.
mfg Torsten
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Stefan Reuther Guest
|
Posted: Tue Jun 22, 2004 6:12 pm Post subject: Re: Template Argument Deduction |
|
|
Kai Ruhnau wrote:
| Quote: | Gegeben sei folgender Code:
template <int N
void f(int foo[N][N], int bar[N])
{
}
|
Die erste Dimension eines Arrays ist nicht Teil der Funktionssignatur.
Das, was da steht, ist identisch zu
template
void f(int foo[][N], int bar[]) { }
bzw.
template <int N>
void f(int (*foo)[N], int* bar) { }
N wird aus der zweiten Dimension des Feldes hergeleitet.
| Quote: | template <int N
void g(int foo[N*N], int bar[N])
{
}
|
Das ist identisch zu
template
void g(int* foo, int* bar) { }
und daraus kann N logischerweise nicht hergeleitet werden. Gleiches gilt
für h und i.
Du suchst vermutlich etwas wie
template<int N>
void g(int (&foo)[N*N], int (&bar)[N]) { }
Das ist semantisch quasi identisch zu deinem Code ('foo' und 'bar' sind
keine Zeiger mehr, du kannst sie also nicht mehr modifizieren (die
Arrays natürlich schon)).
Zumindest diese Signatur von 'g' sollte zulässig sein (der
Template-Parameter N wird eben aus dem zweiten Parameter hergeleitet),
überfordert jedoch die Compiler, die ich momentan unter Windows hier
habe (gcc 2.8.1: 'sorry, not implemented', Borland C++ 5.5: 'internal
compiler error').
Stefan
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
Kai Ruhnau Guest
|
Posted: Tue Jun 22, 2004 9:13 pm Post subject: Re: Template Argument Deduction |
|
|
Stefan Reuther wrote:
[snip]
| Quote: | template
void g(int foo[N*N], int bar[N])
{
}
Das ist identisch zu
template
void g(int* foo, int* bar) { }
und daraus kann N logischerweise nicht hergeleitet werden. Gleiches gilt
für h und i.
Du suchst vermutlich etwas wie
template
void g(int (&foo)[N*N], int (&bar)[N]) { }
|
Jo, vielen Dank (auch an Torsten).
Dass die Array-Zeiger weniger sind, als ich hinschreibe wusste ich noch
nicht. Ich hatte eher gehofft / gedacht, dass C++'s striktere
Typisierung das als separate Funktionen ansieht.
| Quote: | Zumindest diese Signatur von 'g' sollte zulässig sein (der
Template-Parameter N wird eben aus dem zweiten Parameter hergeleitet),
überfordert jedoch die Compiler, die ich momentan unter Windows hier
habe (gcc 2.8.1: 'sorry, not implemented', Borland C++ 5.5: 'internal
compiler error').
|
Der gcc 3.3 hat es anstandslos geschluckt.
Grüße
Kai
--
This signature is left as an exercise for the reader.
--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|