This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/23993] New: Misterious compiler error when accessing a 2d-array in a template class
- From: "bdonner at physik dot tu-muenchen dot de" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 21 Sep 2005 10:33:26 -0000
- Subject: [Bug c++/23993] New: Misterious compiler error when accessing a 2d-array in a template class
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The compiler will give the compile-time error: "invalid lvalue in unary '&'"
The following code not only shows the bug, but also the non-template based
implementation that runs and compiles perfectly. The two dimensional array is
essential to observe the problem. The template code will run and
compile perfectly when the 2d array is replaced by a 1d array.
#include <iostream>
#include <vector>
/*
const int data[2][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7}
};
*/
// For a one dimensional array the code compiles and runs in the policy based
// case
const int data[4] = {
4, 5, 6, 7
};
//This template implementation doesn't even compile due to some strange
//compile-time error.
//---------------- BEGIN TEMPLATE CODE ---------------------------
template <typename T>
class ExtremalInt {
private:
T extremal;
public:
ExtremalInt() : extremal(3) {
}
void operator()(int k) {
int candidate = data[k];
if (extremal < candidate) {
extremal = candidate;
}
}
const int result() const {
return extremal;
}
};
typedef ExtremalInt<int> MaxExtremalInt;
//---------------- END TEMPLATE CODE ---------------------------
/*
//---------------- BEGIN EQUIVALENT CODE ---------------------------
class MaxExtremalInt {
private:
int extremal;
public:
MaxExtremalInt() : extremal(3) {
}
void operator()(int k) {
int candidate = data[1][k];
if (extremal < candidate) {
extremal = candidate;
}
}
const int result() const {
return extremal;
}
};
//---------------- END EQUIVALENT CODE ---------------------------
*/
int main() {
int result;
std::vector<int> array;
array.push_back(1); //5
array.push_back(3); //7
array.push_back(2); //6
MaxExtremalInt sMax;
result = std::for_each(array.begin(), array.end(), sMax).result();
std::cout << "Result: " << result << "\n";
}
--
Summary: Misterious compiler error when accessing a 2d-array in a
template class
Product: gcc
Version: 4.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: bdonner at physik dot tu-muenchen dot de
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23993