Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static struct in C/C++

Tags:

c++

c

syntax

static struct K {  int x; };

Is this valid in C and C++?

like image 429
Henry_1089 Avatar asked Mar 05 '11 17:03

Henry_1089


3 Answers

In C, it's valid but useless.

In C++ it's invalid. You can only specify storage class for objects and functions.

like image 74
Pablo Santa Cruz Avatar answered Oct 31 '22 22:10

Pablo Santa Cruz


Valid in C. Ill-formed in C++

In C++, specifiers extern/static can only be applied to names of objects or functions

Check out

C.1.5 Clause 7: declarations (7.1.1) ISO C++03


like image 29
Prasoon Saurav Avatar answered Oct 31 '22 23:10

Prasoon Saurav


No... That is not valid in C++. An alternative is (C++) : unnamed namespace

namespace 
{
   struct K {  int x; };
}

See this related topic:

Superiority of unnamed namespace over static?

like image 41
Nawaz Avatar answered Oct 31 '22 22:10

Nawaz