PRB: C2279: Braces Not Valid in Function Default Argument List (93401)



The information in this article applies to:

  • Microsoft Visual C++ for Windows, 16-bit edition 1.0
  • Microsoft Visual C++ for Windows, 16-bit edition 1.5
  • Microsoft Visual C++ for Windows, 16-bit edition 1.51
  • Microsoft Visual C++ for Windows, 16-bit edition 1.52
  • Microsoft Visual C++, 32-bit Editions 1.0
  • Microsoft Visual C++, 32-bit Editions 2.0
  • Microsoft Visual C++, 32-bit Editions 2.1
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Editions 4.1
  • Microsoft Visual C++, 32-bit Editions 4.2
  • Microsoft Visual C++, 32-bit Editions 5.0
  • Microsoft Visual C++, 32-bit Editions 6.0
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q93401

SYMPTOMS

When Microsoft C/C++ compiles an application that specifies a structure in the default arguments for a function, the compilation fails with the following error messages:
error C2059: syntax error : '{'
error C2279: cannot use braces to initialize default arguments
If you are using Visual C++ .NET compiler, the following compiler errors will be generated instead of the above two compiler errors.

error C2143: syntax error : missing ')' before '{'
error C2059: syntax error : ')'

CAUSE

According to the C++ grammar, the default value for an argument must be an expression. An initializer list, such as that used to initialize a structure, is not an expression.

RESOLUTION

Define a constructor to perform the required initialization.

MORE INFORMATION

The first code example demonstrates the errors listed above. The second code example demonstrates the correct method to initialize the structure.

Code Sample 1

/*
 * Compile options needed: /c
 */ 

struct ag_type
{
   int a;
   float b;
};

void func(ag_type arg = {5, 7.0});
				

Code Sample 2

/*
 * Compile options needed: /c
 */ 

struct ag_type
{
   int a;
   float b;
   ag_type(int aa, float bb) : a(aa), b(bb) {}
};

void func(ag_type arg = ag_type(5, 7.0));
				

Modification Type:MinorLast Reviewed:7/5/2005
Keywords:kbCompiler kbCPPonly kbprb KB93401