public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "bdonner at physik dot tu-muenchen dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/23993] New: Misterious compiler error when accessing a 2d-array in a template class
Date: Wed, 21 Sep 2005 10:33:00 -0000	[thread overview]
Message-ID: <20050921103324.23993.bdonner@physik.tu-muenchen.de> (raw)

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


             reply	other threads:[~2005-09-21 10:33 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-21 10:33 bdonner at physik dot tu-muenchen dot de [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050921103324.23993.bdonner@physik.tu-muenchen.de \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).