Hi
Th***********@icp.uni-stuttgart.de wrote:
i have to get an aprx. 9 year old code running. back then id did
compile but now it doesn't.
9 year old? I've seen younger code that looked a lot worse.
i have broken down the problem to the following technique which is
used quit freq. in the code.
don't ask me why it was used or if it makes sense, i don't even know
the guy who wrote it personally.
If you have any information about:
a) why this is not working anymore.
Because of dependent name lookup. At least GCC used to do name lookup wrong
for dependent names, which made the code compile.
b) when did this stop working
Depends on the compiler.
c) can i use a compiler switch to accept the code (will it run?)
It might be possible, but it would be better to fix the code (which is
luckily rather easy)
#include <iostream>
using namespace std;
template<class T,int dim>
class TBase
{
public:
TBase(T val){pvar[dim-1]=val;};
T show_B(void){ return pvar[dim-1];};
protected:
T pvar[dim];
};
template <class TC>
class TClass: public TBase<TC,1>
{
public:
TClass(TC bval):TBase<TC,1>(bval){ pvar[0]=bval; };
The problem is that pvar is not a dependent name (it does not depend on any
template parameters), so it is looked up when the template definition is
processed. At that point, it is uncertain what template base classes look
like, so they are not included in lookup. The easy fix is to make the name
dependent by writing e.g. this->pvar instead:
TClass(TC bval) : TBase<TC,1>(bval) { this->pvar[0] = bval; }
TC show_A(void){return pvar[0];};
The same here:
TC show_A() { return this->pvar[0]; }
Markus