 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dkouroun@cc.uoi.gr Guest
|
Posted: Tue Nov 08, 2005 10:38 am Post subject: some help from experts is needed here. The compiler behaves |
|
|
Dear experts,
when I compile the following code with g++-4.1,
the latest version, or with g++-3.4.4 I get really
strange errors. It seems that the compiler cannot
understand the explicit specialization and somehow
it reinterprets it like operator '<'. On the other hand
intel's icpc compiler compile this code really smoothly.
Does anybody have any idea what the problem might
be here? Code follows:
****************************************************
enum Color
{
_A = 0,
_B = 1,
};
enum Brand
{
BrA = 1,
BrB = 2,
};
enum Tire
{
Pirelli = 0,
BRIDGESTON = 1
};
enum MACHINE
{
AUDI = 0,
};
template
Brand D1,
Tire P1,
Brand D2,
Tire P2>
class Speed;
template<>
class Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>
{
public:
Speed() {}
template<Color C>
double RunFast(int i, int k);
};
template<> inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::RunFast<_A>(int i, int
k)
{
return 0.3*i*i*k;
}
template<> inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::RunFast<_B>(int i, int
k)
{
return
0.5*k*k*i;
}
template<long int N, MACHINE S, typename Expr>
class Breaks;
template<typename Expr>
class Breaks<3, AUDI, Expr>
{
private:
Expr& expr_;
public:
Breaks(Expr& expr)
: expr_(expr)
{
}
template<Color C>
double speadup(const int i)
{
return (
expr_.speadup<C>(i, 0) +
( expr_.speadup<C>(i, 1) +
expr_.speadup<C>(i, 2) +
expr_.speadup<C>(i, 3) ));
}
};
template<Brand D, MACHINE S, Tire P>
class Accel
{
public:
typedef Speed<S, D, P, D, P> SpeedType;
public:
SpeedType& speed;
Breaks<3, S, Accel> run;
public:
~Accel();
Accel(SpeedType& rspeed);
void calculate();
template<Color C> inline double
speadup(const int i, const int k);
};
template<Brand D, MACHINE S, Tire P>
Accel<D, S, P>::~Accel()
{
}
template<Brand D, MACHINE S, Tire P>
Accel<D, S, P>::Accel(SpeedType& rspeed)
: speed(rspeed), run(*this)
{
}
template<Brand D, MACHINE S, Tire P>
template<Color C> inline double
Accel<D, S, P>::speadup(const int i, const int k)
{
return speed.RunFast<C>(i, k);
}
template<> inline void
Accel<BrB, AUDI, BRIDGESTON>::calculate()
{
int i, j;
i = 10;
j = 20;
double a = 0.0;
a += run.speadup<_A>(i);
a += run.speadup<_B>(i);
}
int main()
{
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON> speed;
Accel<BrB, AUDI, BRIDGESTON> poi(speed);
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dkouroun@cc.uoi.gr Guest
|
Posted: Tue Nov 08, 2005 2:51 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
In what you see above there is a typo in 'speedup' which
has been written as 'speadup'. This is not the problem in
the code.
The correct code follows:
---------------------------------
enum Color
{
_A = 0,
_B = 1,
};
enum Brand
{
BrA = 1,
BrB = 2,
};
enum Tire
{
Pirelli = 0,
BRIDGESTON = 1
};
enum MACHINE
{
AUDI = 0,
};
template<MACHINE S,
Brand D1,
Tire P1,
Brand D2,
Tire P2>
class Car;
template<>
class Car<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>
{
public:
Car() {}
template<Color C>
double Paint(int i, int k);
};
template<> inline double
Car<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::Paint<_A>(int i, int k)
{
return 0.3*i*i*k;
}
template<> inline double
Car<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::Paint<_B>(int i, int k)
{
return
0.5*k*k*i;
}
template<long int N, MACHINE S, typename Expr>
class Accelaration;
template<typename Expr>
class Accelaration<3, AUDI, Expr>
{
private:
Expr& expr_;
public:
Accelaration(Expr& expr)
: expr_(expr)
{
}
template<Color C>
double speedup(const int i)
{
return (
expr_.speedup<C>(i, 0) +
( expr_.speedup<C>(i, 1) +
expr_.speedup<C>(i, 2) +
expr_.speedup<C>(i, 3) ));
}
};
template<Brand D, MACHINE S, Tire P>
class Accel
{
public:
typedef Car<S, D, P, D, P> CarType;
public:
CarType& car;
Accelaration<3, S, Accel> run;
public:
~Accel();
Accel(CarType& rcar);
void calculate();
template<Color C> inline double
speedup(const int i, const int k);
};
template<Brand D, MACHINE S, Tire P>
Accel<D, S, P>::~Accel()
{
}
template<Brand D, MACHINE S, Tire P>
Accel<D, S, P>::Accel(CarType& rcar)
: car(rcar), run(*this)
{
}
template<Brand D, MACHINE S, Tire P>
template<Color C> inline double
Accel<D, S, P>::speedup(const int i, const int k)
{
return car.Paint<C>(i, k);
}
template<> inline void
Accel<BrB, AUDI, BRIDGESTON>::calculate()
{
int i, j;
i = 10;
j = 20;
double a = 0.0;
a += run.speedup<_A>(i);
a += run.speedup<_B>(i);
}
int main()
{
Car<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON> car;
Accel<BrB, AUDI, BRIDGESTON> poi(car);
return 0;
}
and when I try to compile with:
g++-4.1 or g++-3.4.4
I get
main.cc: In member function 'double Accel<D, S, P>::speedup(int, int)
[with Color C = _A, Brand D = BrB, MACHINE S = AUDI, Tire P =
BRIDGESTON]':
main.cc:79: instantiated from 'double Accelaration<3l, AUDI,
Expr>::speedup(int) [with Color C = _A, Expr = Accel<BrB, AUDI,
BRIDGESTON>]'
main.cc:129: instantiated from here
main.cc:119: error: no match for 'operator<' in '((Accel
BRIDGESTON>*)this)->Accel<BrB, AUDI, BRIDGESTON>::car->Car<AUDI, BrB,
BRIDGESTON, BrB, BRIDGESTON>::Paint < _A'
main.cc: In member function 'double Accel
[with Color C = _B, Brand D = BrB, MACHINE S = AUDI, Tire P =
BRIDGESTON]':
main.cc:79: instantiated from 'double Accelaration<3l, AUDI,
Expr>::speedup(int) [with Color C = _B, Expr = Accel<BrB, AUDI,
BRIDGESTON>]'
main.cc:130: instantiated from here
main.cc:119: error: no match for 'operator<' in '((Accel
BRIDGESTON>*)this)->Accel<BrB, AUDI, BRIDGESTON>::car->Car<AUDI, BrB,
BRIDGESTON, BrB, BRIDGESTON>::Paint < _B'
while icpc compiles the code as usual.
What is wrong here?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Tue Nov 08, 2005 2:51 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
[email]dkouroun (AT) cc (DOT) uoi.gr[/email] wrote:
| Quote: | Dear experts,
when I compile the following code with g++-4.1,
the latest version, or with g++-3.4.4 I get really
strange errors. It seems that the compiler cannot
understand the explicit specialization and somehow
it reinterprets it like operator '<'. On the other hand
intel's icpc compiler compile this code really smoothly.
Does anybody have any idea what the problem might
be here? Code follows:
|
Next time, please state the exact error code and the line that produces
the error.
| Quote: | template<> inline void
Accel<BrB, AUDI, BRIDGESTON>::calculate()
{
int i, j;
i = 10;
j = 20;
double a = 0.0;
a += run.speadup<_A>(i);
a += run.speadup<_B>(i);
}
|
rewrite the last two lines as follows:
a += run.template speadup<_A>(i);
a += run.template speadup<_B>(i);
the reason for this exotic syntax has been discussed several times in
this newsgroup and every decent C++ book I read has a section on it.
In this particular case, withouth the extra "template" keyword, the
compiler *must* treat the following '<' as a less-than operator, so your
compiler is definitely not drunk at all.
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dkouroun@cc.uoi.gr Guest
|
Posted: Tue Nov 08, 2005 6:16 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
In what you see above there is a typo in 'speedup' which
has been written as 'speadup'. This is not the problem in
the code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Tue Nov 08, 2005 6:19 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
[email]dkouroun (AT) cc (DOT) uoi.gr[/email] wrote:
| Quote: | enum Color
{
_A = 0,
_B = 1,
};
|
Wrong. All identifiers beginnig with an underscore and a capital are
reserved. IOW, your code is wrong and the compiler is right to do what it
wants. It is in fact possible that these two are used as macros somewhere,
which will of course mess up your code. Other compilers or versions may or
may not use these macros, which explains the different behaviour you see.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Wed Nov 09, 2005 1:49 am Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
[email]dkouroun (AT) cc (DOT) uoi.gr[/email] wrote:
| Quote: | Dear experts,
when I compile the following code with g++-4.1,
the latest version, or with g++-3.4.4 I get really
strange errors. It seems that the compiler cannot
understand the explicit specialization and somehow
it reinterprets it like operator '<'. On the other hand
intel's icpc compiler compile this code really smoothly.
Does anybody have any idea what the problem might
be here? Code follows:
****************************************************
enum Color
{
_A = 0,
_B = 1,
};
enum Brand
{
BrA = 1,
BrB = 2,
};
enum Tire
{
Pirelli = 0,
BRIDGESTON = 1
};
enum MACHINE
{
AUDI = 0,
};
template
Brand D1,
Tire P1,
Brand D2,
Tire P2
class Speed;
template
class Speed
{
public:
Speed() {}
template
double RunFast(int i, int k);
};
template<> inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::RunFast<_A>(int i, int
k)
{
return 0.3*i*i*k;
}
template<> inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::RunFast<_B>(int i, int
k)
{
return
0.5*k*k*i;
}
|
Note that both the _A and _B identifiers have a leading underscore
followed by a capital letter. Such identifiers are reserved by the
implementation and should not be declared in user code.
Specializing Speed and then specializing its RunFast member template is
apparently not legal. A simpler approach would be to declare RunFast in
the master template but implement it only in specializations:
template<MACHINE S,
Brand D1,
Tire P1,
Brand D2,
Tire P2>
class Speed
{
public:
Speed() {}
template<Color C>
double RunFast(int i, int k);
};
template<>
template<>
inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::
RunFast<_A>(int i, int k)
{
return 0.3*i*i*k;
}
template<>
template<>
inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::
RunFast<_B>(int i, int k)
{
return 0.5*k*k*i;
}
Not implementing RunFast() in the master template of Speed should still
prevent non-specialized Speed types from being instantiated.
| Quote: | template<long int N, MACHINE S, typename Expr
class Breaks;
template
class Breaks<3, AUDI, Expr
{
private:
Expr& expr_;
public:
Breaks(Expr& expr)
: expr_(expr)
{
}
template
double speadup(const int i)
{
return (
expr_.speadup
( expr_.speadup<C>(i, 1) +
expr_.speadup<C>(i, 2) +
expr_.speadup<C>(i, 3) ));
}
};
template<Brand D, MACHINE S, Tire P
class Accel
{
public:
typedef Speed
public:
SpeedType& speed;
Breaks<3, S, Accel> run;
public:
~Accel();
Accel(SpeedType& rspeed);
void calculate();
template<Color C> inline double
speadup(const int i, const int k);
};
template<Brand D, MACHINE S, Tire P
Accel
{
}
template<Brand D, MACHINE S, Tire P
Accel
: speed(rspeed), run(*this)
{
}
template<Brand D, MACHINE S, Tire P
template
Accel<D, S, P>::speadup(const int i, const int k)
{
return speed.RunFast<C>(i, k);
}
|
The speadup method is missing the template keyword:
template<Brand D, MACHINE S, Tire P>
template<Color C> inline double
Accel<D, S, P>::speadup(const int i, const int k)
{
return speed.template RunFast<C>(i, k);
}
Those changes should be enough to compile this program.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Wed Nov 09, 2005 1:55 am Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
[email]dkouroun (AT) cc (DOT) uoi.gr[/email] wrote:
| Quote: |
and when I try to compile with:
g++-4.1 or g++-3.4.4
I get
main.cc: In member function 'double Accel<D, S, P>::speedup(int, int)
snip
|
In my post I reported a problem in function calculate(), but it seems
that speedup() also has the same problem. It should be written as:
template<Color C>
double speedup(const int i)
{
return (
expr_.template speedup<C>(i, 0) +
( expr_.template speedup<C>(i, 1) +
expr_.template speedup<C>(i, 2) +
expr_.template speedup<C>(i, 3) ));
}
Notice the ".template" syntax. And there's again the same problem in
Accel<D, S, P>::speedup().
| Quote: |
while icpc compiles the code as usual.
What is wrong here?
|
icpc is wrong. g++ is right: the ".template" syntax is required and was
missing.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dkouroun@cc.uoi.gr Guest
|
Posted: Wed Nov 09, 2005 4:35 am Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
Dear Uli,
thank you for your time. I checked what you suggested
changing _A to CA and _B to CB but unfortunately this is not the
case. Look at the errors this time:
main.cc: In member function `double Accel<D, S, P>::speedup(int, int)
[with Color C = CA, Brand D = BrB, MACHINE S = AUDI, Tire P =
BRIDGESTON]':
main.cc:75: instantiated from `double Accelaration<3l, AUDI,
Expr>::speedup(int) [with Color C = CA, Expr = Accel< BrB, AUDI,
BRIDGESTON>]'
main.cc:129: instantiated from here
main.cc:119: error: no match for 'operator<' in '((Accel< BrB, AUDI,
BRIDGESTON>*)this)->Accel< BrB, AUDI, BRIDGESTON>::car->Car< AUDI,
BrB, BRIDGESTON, BrB, BRIDGESTON>::Paint < CA'
main.cc: In member function `double Accel
[with Color C = CB, Brand D = BrB, MACHINE S = AUDI, Tire P =
BRIDGESTON]':
main.cc:75: instantiated from `double Accelaration<3l, AUDI,
Expr>::speedup(int) [with Color C = CB, Expr = Accel< BrB, AUDI,
BRIDGESTON>]'
main.cc:130: instantiated from here
main.cc:119: error: no match for 'operator<' in '((Accel< BrB, AUDI,
BRIDGESTON>*)this)->Accel< BrB, AUDI, BRIDGESTON>::car->Car< AUDI,
BrB, BRIDGESTON, BrB, BRIDGESTON>::Paint < CB'
So, your piece of advice might be good as a general rule,
and it surely is but in this case it seems that the source of
the problem is not what you suggest! Thank you anyway
for your time!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Martin Bonner Guest
|
Posted: Wed Nov 09, 2005 1:08 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
[email]dkouroun (AT) cc (DOT) uoi.gr[/email] wrote:
| Quote: | Dear experts,
when I compile the following code with g++-4.1,
the latest version, or with g++-3.4.4 I get really
strange errors. It seems that the compiler cannot
understand the explicit specialization and somehow
it reinterprets it like operator '<'. On the other hand
intel's icpc compiler compile this code really smoothly.
Does anybody have any idea what the problem might
be here? Code follows:
|
[SNIP]
Can you post a minimal example, and the resulting error message?
For example:
template<> inline double
Speed<AUDI, BrB, BRIDGESTON, BrB, BRIDGESTON>::RunFast<_A>
(int i, int k)
{
return 0.3*i*i*k;
}
I am sure the Speed class only needs one template parameter to show the
problem, and RunFast only needs one argument:
template<>
inline double Speed<AUDI>::RunFast<_A>(int i)
{
return i;
}
One thing you /could/ try is converting the above to:
template<>
inline double Speed<AUDI>::template<> RunFast<_A>(int i)
{
return i;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Wed Nov 09, 2005 2:20 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
<dkouroun (AT) cc (DOT) uoi.gr> wrote
| Quote: | In what you see above there is a typo in 'speedup' which
has been written as 'speadup'. This is not the problem in
the code.
The correct code follows:
---------------------------------
enum Color
{
_A = 0,
_B = 1,
};
|
Don't use reserved names (e.g., _A). Don't use non-standard trailing comma
in enumerators. And everything should compile. It's also a good idea to use
all UPPERCASE names for macros only.
-- Gene Bushuyev ~ Cadence Design Systems
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Fri Nov 11, 2005 9:05 am Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
Martin Bonner wrote:
| Quote: | I am sure the Speed class only needs one template parameter to show the
problem, and RunFast only needs one argument:
template
inline double Speed<AUDI>::RunFast<_A>(int i)
{
return i;
}
|
Wrong. The full specialization of Speed needs all template parameters.
| Quote: | One thing you /could/ try is converting the above to:
template
inline double Speed<AUDI>::template<> RunFast<_A>(int i)
{
return i;
}
|
That's definitely a syntax error. There's no way "template <>" could fit
there.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
dkouroun@cc.uoi.gr Guest
|
Posted: Fri Nov 11, 2005 2:59 pm Post subject: Re: some help from experts is needed here. The compiler beha |
|
|
Greg can you explain why the .template keyword is
not needed necessarily in
template<Color C>
double speedup(const int i)
{
return (
expr_.speedup<C>(i, 0) +
( expr_.speedup<C>(i, 1) +
expr_.speedup<C>(i, 2) +
expr_.speedup<C>(i, 3) ));
}
member of Accelaration Class? In fact I only added the .template
keyword where you suggested and the program compiled with g++.
But I cannot understand why it is not needed above?
Any ideas?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|