public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/23993] New: Misterious compiler error when accessing a 2d-array in a template class
@ 2005-09-21 10:33 bdonner at physik dot tu-muenchen dot de
  2005-09-21 10:41 ` [Bug c++/23993] " bdonner at physik dot tu-muenchen dot de
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: bdonner at physik dot tu-muenchen dot de @ 2005-09-21 10:33 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2005-10-01  1:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-21 10:33 [Bug c++/23993] New: Misterious compiler error when accessing a 2d-array in a template class bdonner at physik dot tu-muenchen dot de
2005-09-21 10:41 ` [Bug c++/23993] " bdonner at physik dot tu-muenchen dot de
2005-09-21 11:24 ` [Bug c++/23993] [4.0/4.1 Regression] " pinskia at gcc dot gnu dot org
2005-09-21 12:19 ` pinskia at gcc dot gnu dot org
2005-09-21 13:10 ` mmitchel at gcc dot gnu dot org
2005-09-22  0:10 ` [Bug c++/23993] [4.0/4.1 Regression] Mysterious " cvs-commit at gcc dot gnu dot org
2005-09-22  0:11 ` cvs-commit at gcc dot gnu dot org
2005-09-22  0:11 ` cvs-commit at gcc dot gnu dot org
2005-09-22  0:12 ` mmitchel at gcc dot gnu dot org
2005-10-01  1:25 ` mmitchel at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).