Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a VS2008 bug? Functional style variable initialisation

In the code below, the line

const char * const * eNames (names+cntNames); 

results in a C2061 error in Visual Studio 2008:

syntax error : identifier 'identifier' - The compiler found an identifier where it wasn't expected. Make sure that identifier is declared before you use it. An initializer may be enclosed by parentheses. To avoid this problem, enclose the declarator in parentheses or make it a typedef. This error could also be caused when the compiler detects an expression as a class template argument; use typename to tell the compiler it is a type.

If I change to

const char * const * eNames = names+cntNames; 

it doesn't complain. Is this a compiler bug? If not, why the complaint?

My About box says: Version 9.0.30729.1 SP

My colleague with GCC does not see this error.

#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

namespace ns1 {

   struct str_eq_to
   {
      str_eq_to(const std::string& s) : s_(s) {}
      bool operator()(const char* x) const { return s_.compare(x)==0; }
      const std::string& s_;
   };

   static bool getNameIndex(const char * const * names, size_t cntNames, const std::string& nm, int &result)
   {
      const char * const * eNames (names+cntNames);  //VS2008 error C2061: syntax error : identifier 'names'
      const char * const * p = std::find_if(names, eNames, str_eq_to(nm));
      if(p==eNames) return false;
      result = p-names;
      return true;
   }

} //namespace ns1


int main() {

   const char * const names[] = {"Apple", "Orange","Plum"};
   std::string str = "Plum";
   int res;

   ns1::getNameIndex(names, 3, str, res);
   std::cout << str << " is at index " << res << std::endl; 
   return 0;
}
like image 990
Angus Comber Avatar asked May 09 '13 14:05

Angus Comber


1 Answers

This is most definitely a compiler bug. Witness:

extern char** a;
typedef char* cp;
char** c(a);      // error
cp* c1(a);        // no error
char** c2(c1);    // error
cp* n(0);         // no error
char** n2(0);     // error
like image 149
n. 1.8e9-where's-my-share m. Avatar answered Oct 02 '22 21:10

n. 1.8e9-where's-my-share m.