sizeof in static const member initialization
I have such code:
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
I got compiler error in VC++ and no errors in IAR.
Which compiler is right, what C++ standart says about it?
c++ visual-studio-2013 visual-studio-2005
add a comment |
I have such code:
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
I got compiler error in VC++ and no errors in IAR.
Which compiler is right, what C++ standart says about it?
c++ visual-studio-2013 visual-studio-2005
3
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43
add a comment |
I have such code:
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
I got compiler error in VC++ and no errors in IAR.
Which compiler is right, what C++ standart says about it?
c++ visual-studio-2013 visual-studio-2005
I have such code:
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
I got compiler error in VC++ and no errors in IAR.
Which compiler is right, what C++ standart says about it?
c++ visual-studio-2013 visual-studio-2005
c++ visual-studio-2013 visual-studio-2005
edited Nov 28 '18 at 13:33
gsamaras
52k24107191
52k24107191
asked Nov 28 '18 at 13:19
vadim b.vadim b.
63
63
3
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43
add a comment |
3
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43
3
3
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43
add a comment |
4 Answers
4
active
oldest
votes
Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.
9.4 [class.static] (emphasis mine)
If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).
add a comment |
What do you have?
You have the definition of a class
named A
.
Your class has a unsigned long
named a
.
Your class has a static const unsigned long
named b
.
In certain C++ compilers, static and non-static members of a class
can't be mixed, specially in the definition stage.
What do you want?
static const unsigned long b = sizeof(unsigned long);
This is not exactly what you want, but this is how a smart compiler try to figure out.
WHY???
Because static members doesn't limit their scope to the object definition. They overpass the object scope and can be accessed from everywhere simply outputting A::b
in the console using std::cout << A::b << std::endl
, for example.
Clang doesn't accept this construction, GCC does (both with -g -std=c++98
)
MSVC 19.14 (visual studio 15.7) doesn't accept it, too, but visual studio 15.8, with MSVC 19.15, does.
Choose carefully.
Where I check all this stuff?
Here I check lots of compilers: https://godbolt.org/
This is one method, but you must evade this kind of hacks in the future.
Magic code to check and blame
The blame part is for mixing C and C++. It's only for compile with older versions without any checks:
#include <stdio.h>
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
int main (void)
{
printf ( "Hello World" ); // I strongly not reccomend using printf's in C++ code.
printf ( "%d", A::b ); // This is only to fill a functional example, not a rightful one.
return 0;
}
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
add a comment |
StoryTeller's answer specifies why this didn't work on visual-studio-2005. Namely because it wasn't supported until c++11.
As far as visual-studio-2013 it's not fully c++11 compliant. But I've validated that this code works around the deficiency:
static const unsigned long b = sizeof(decltype(a))
If you want something that will work with visual-studio-2005 as well, consider making b
a global, instead of a static member of A
:
const unsigned long b = sizeof(A().a)
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is thatsizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.
– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
add a comment |
static
const
-qualified member in-class initialisers using non-static
members were not part of the C++ standard until C++11.
The earliest MSVC compiler that fully supports C++11 is MSVC2017.
That compiler will compile your code correctly.
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!
– Jonathan Mee
Nov 28 '18 at 15:27
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520397%2fsizeof-in-static-const-member-initialization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.
9.4 [class.static] (emphasis mine)
If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).
add a comment |
Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.
9.4 [class.static] (emphasis mine)
If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).
add a comment |
Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.
9.4 [class.static] (emphasis mine)
If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).
Your MSVS versions are quite old, so based on that, and assuming they default to C++03, they are correct to reject your code. I'll quote n1905, which for our purposes is pretty close to the C++03 standard.
9.4 [class.static] (emphasis mine)
If an unqualified-id (5.1) is used in the definition of a static
member following the member’s declarator-id, and name lookup (3.4.1)
finds that the unqualified-id refers to a static member, enumerator,
or nested type of the member’s class (or of a base class of the
member’s class), the unqualified-id is transformed into a qualified-id
expression in which the nested-name-specifier names the class scope
from which the member is referenced. The definition of a static member
shall not use directly the names of the non-static members of its
class or of a base class of its class (including as operands of the
sizeof operator). The definition of a static member may only refer to
these members to form pointer to members (5.3.1) or with the class
member access syntax (5.2.5).
edited Nov 28 '18 at 13:54
answered Nov 28 '18 at 13:45
StoryTellerStoryTeller
103k12216280
103k12216280
add a comment |
add a comment |
What do you have?
You have the definition of a class
named A
.
Your class has a unsigned long
named a
.
Your class has a static const unsigned long
named b
.
In certain C++ compilers, static and non-static members of a class
can't be mixed, specially in the definition stage.
What do you want?
static const unsigned long b = sizeof(unsigned long);
This is not exactly what you want, but this is how a smart compiler try to figure out.
WHY???
Because static members doesn't limit their scope to the object definition. They overpass the object scope and can be accessed from everywhere simply outputting A::b
in the console using std::cout << A::b << std::endl
, for example.
Clang doesn't accept this construction, GCC does (both with -g -std=c++98
)
MSVC 19.14 (visual studio 15.7) doesn't accept it, too, but visual studio 15.8, with MSVC 19.15, does.
Choose carefully.
Where I check all this stuff?
Here I check lots of compilers: https://godbolt.org/
This is one method, but you must evade this kind of hacks in the future.
Magic code to check and blame
The blame part is for mixing C and C++. It's only for compile with older versions without any checks:
#include <stdio.h>
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
int main (void)
{
printf ( "Hello World" ); // I strongly not reccomend using printf's in C++ code.
printf ( "%d", A::b ); // This is only to fill a functional example, not a rightful one.
return 0;
}
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
add a comment |
What do you have?
You have the definition of a class
named A
.
Your class has a unsigned long
named a
.
Your class has a static const unsigned long
named b
.
In certain C++ compilers, static and non-static members of a class
can't be mixed, specially in the definition stage.
What do you want?
static const unsigned long b = sizeof(unsigned long);
This is not exactly what you want, but this is how a smart compiler try to figure out.
WHY???
Because static members doesn't limit their scope to the object definition. They overpass the object scope and can be accessed from everywhere simply outputting A::b
in the console using std::cout << A::b << std::endl
, for example.
Clang doesn't accept this construction, GCC does (both with -g -std=c++98
)
MSVC 19.14 (visual studio 15.7) doesn't accept it, too, but visual studio 15.8, with MSVC 19.15, does.
Choose carefully.
Where I check all this stuff?
Here I check lots of compilers: https://godbolt.org/
This is one method, but you must evade this kind of hacks in the future.
Magic code to check and blame
The blame part is for mixing C and C++. It's only for compile with older versions without any checks:
#include <stdio.h>
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
int main (void)
{
printf ( "Hello World" ); // I strongly not reccomend using printf's in C++ code.
printf ( "%d", A::b ); // This is only to fill a functional example, not a rightful one.
return 0;
}
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
add a comment |
What do you have?
You have the definition of a class
named A
.
Your class has a unsigned long
named a
.
Your class has a static const unsigned long
named b
.
In certain C++ compilers, static and non-static members of a class
can't be mixed, specially in the definition stage.
What do you want?
static const unsigned long b = sizeof(unsigned long);
This is not exactly what you want, but this is how a smart compiler try to figure out.
WHY???
Because static members doesn't limit their scope to the object definition. They overpass the object scope and can be accessed from everywhere simply outputting A::b
in the console using std::cout << A::b << std::endl
, for example.
Clang doesn't accept this construction, GCC does (both with -g -std=c++98
)
MSVC 19.14 (visual studio 15.7) doesn't accept it, too, but visual studio 15.8, with MSVC 19.15, does.
Choose carefully.
Where I check all this stuff?
Here I check lots of compilers: https://godbolt.org/
This is one method, but you must evade this kind of hacks in the future.
Magic code to check and blame
The blame part is for mixing C and C++. It's only for compile with older versions without any checks:
#include <stdio.h>
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
int main (void)
{
printf ( "Hello World" ); // I strongly not reccomend using printf's in C++ code.
printf ( "%d", A::b ); // This is only to fill a functional example, not a rightful one.
return 0;
}
What do you have?
You have the definition of a class
named A
.
Your class has a unsigned long
named a
.
Your class has a static const unsigned long
named b
.
In certain C++ compilers, static and non-static members of a class
can't be mixed, specially in the definition stage.
What do you want?
static const unsigned long b = sizeof(unsigned long);
This is not exactly what you want, but this is how a smart compiler try to figure out.
WHY???
Because static members doesn't limit their scope to the object definition. They overpass the object scope and can be accessed from everywhere simply outputting A::b
in the console using std::cout << A::b << std::endl
, for example.
Clang doesn't accept this construction, GCC does (both with -g -std=c++98
)
MSVC 19.14 (visual studio 15.7) doesn't accept it, too, but visual studio 15.8, with MSVC 19.15, does.
Choose carefully.
Where I check all this stuff?
Here I check lots of compilers: https://godbolt.org/
This is one method, but you must evade this kind of hacks in the future.
Magic code to check and blame
The blame part is for mixing C and C++. It's only for compile with older versions without any checks:
#include <stdio.h>
class A
{
public:
unsigned long a;
static const unsigned long b = sizeof(a); // "error C2327: 'A::a' : is not a type name, static, or enumerator" in VC++
};
int main (void)
{
printf ( "Hello World" ); // I strongly not reccomend using printf's in C++ code.
printf ( "%d", A::b ); // This is only to fill a functional example, not a rightful one.
return 0;
}
answered Nov 28 '18 at 13:45
José Manuel RamosJosé Manuel Ramos
273210
273210
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
add a comment |
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
This answer does provide some circumstantial evidence of which behavior is correct. As such I feel bad about the fact someone downvoted here, especially when such effort was put into this answer. So have a +1.
– Jonathan Mee
Nov 28 '18 at 14:46
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
I don't feel bad about downvoting. I'm feeling good somebody could read it and understands it, even if they downvote or upvote that. The standards are not the real world, but the compilers out there, are.
– José Manuel Ramos
Dec 6 '18 at 12:07
add a comment |
StoryTeller's answer specifies why this didn't work on visual-studio-2005. Namely because it wasn't supported until c++11.
As far as visual-studio-2013 it's not fully c++11 compliant. But I've validated that this code works around the deficiency:
static const unsigned long b = sizeof(decltype(a))
If you want something that will work with visual-studio-2005 as well, consider making b
a global, instead of a static member of A
:
const unsigned long b = sizeof(A().a)
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is thatsizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.
– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
add a comment |
StoryTeller's answer specifies why this didn't work on visual-studio-2005. Namely because it wasn't supported until c++11.
As far as visual-studio-2013 it's not fully c++11 compliant. But I've validated that this code works around the deficiency:
static const unsigned long b = sizeof(decltype(a))
If you want something that will work with visual-studio-2005 as well, consider making b
a global, instead of a static member of A
:
const unsigned long b = sizeof(A().a)
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is thatsizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.
– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
add a comment |
StoryTeller's answer specifies why this didn't work on visual-studio-2005. Namely because it wasn't supported until c++11.
As far as visual-studio-2013 it's not fully c++11 compliant. But I've validated that this code works around the deficiency:
static const unsigned long b = sizeof(decltype(a))
If you want something that will work with visual-studio-2005 as well, consider making b
a global, instead of a static member of A
:
const unsigned long b = sizeof(A().a)
StoryTeller's answer specifies why this didn't work on visual-studio-2005. Namely because it wasn't supported until c++11.
As far as visual-studio-2013 it's not fully c++11 compliant. But I've validated that this code works around the deficiency:
static const unsigned long b = sizeof(decltype(a))
If you want something that will work with visual-studio-2005 as well, consider making b
a global, instead of a static member of A
:
const unsigned long b = sizeof(A().a)
edited Nov 28 '18 at 14:31
answered Nov 28 '18 at 14:11
Jonathan MeeJonathan Mee
22k1065176
22k1065176
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is thatsizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.
– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
add a comment |
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is thatsizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.
– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
1
1
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
Keyword is should. And I'm not sure C++11 was the default standard for MSVC2013 back then.
– StoryTeller
Nov 28 '18 at 14:19
1
1
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
According to this source, C++11 is not entirely supported even in MSVC2015.
– Algirdas Preidžius
Nov 28 '18 at 14:21
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is that
sizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.– Jonathan Mee
Nov 28 '18 at 14:30
@AlgirdasPreidžius You're not wrong... Even VS2015 has some outstanding C++11 deficiencies. Or maybe I should just say C++ standard deficiencies :( I'll reword just to be clear. What I was trying to communicate is that
sizeof(a)
is actually valid code now... It's just a Visual Studio discrepancy that prevented it from compiling.– Jonathan Mee
Nov 28 '18 at 14:30
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
@StoryTeller Yeah, I've clarified. As has been pointed out VS2013 didn't even claim to be C++11 compliant.
– Jonathan Mee
Nov 28 '18 at 14:33
add a comment |
static
const
-qualified member in-class initialisers using non-static
members were not part of the C++ standard until C++11.
The earliest MSVC compiler that fully supports C++11 is MSVC2017.
That compiler will compile your code correctly.
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!
– Jonathan Mee
Nov 28 '18 at 15:27
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
add a comment |
static
const
-qualified member in-class initialisers using non-static
members were not part of the C++ standard until C++11.
The earliest MSVC compiler that fully supports C++11 is MSVC2017.
That compiler will compile your code correctly.
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!
– Jonathan Mee
Nov 28 '18 at 15:27
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
add a comment |
static
const
-qualified member in-class initialisers using non-static
members were not part of the C++ standard until C++11.
The earliest MSVC compiler that fully supports C++11 is MSVC2017.
That compiler will compile your code correctly.
static
const
-qualified member in-class initialisers using non-static
members were not part of the C++ standard until C++11.
The earliest MSVC compiler that fully supports C++11 is MSVC2017.
That compiler will compile your code correctly.
answered Nov 28 '18 at 14:47
BathshebaBathsheba
181k27256384
181k27256384
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!
– Jonathan Mee
Nov 28 '18 at 15:27
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
add a comment |
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!
– Jonathan Mee
Nov 28 '18 at 15:27
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.
decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!– Jonathan Mee
Nov 28 '18 at 15:27
I believe this to be correct (though I don't have VS2015 to check.) I just couldn't figure out the feature name that Microsoft gave to this portion of the spec, so I couldn't find anything authoritative on the timeline in which support was gained.
decltype
can be used to workaround this deficiency circa VS 2012. So I'd really like to read about the limitation... If you could provide a link to source your statements it's worth a +1 to ya!– Jonathan Mee
Nov 28 '18 at 15:27
1
1
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
@JonathanMee: Informally, you could check the value of __cplusplus on the compiler: MSVC2017 is the earliest one that has this to a C++11 value. Probably only worth 1/2 in integer arithmetic ;-)
– Bathsheba
Nov 28 '18 at 15:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520397%2fsizeof-in-static-const-member-initialization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Which MSVC version are you using?
– Bathsheba
Nov 28 '18 at 13:22
MSVS 2013 and MSVS 2005.
– vadim b.
Nov 28 '18 at 13:25
I'd imagine this means that you are working with an up to date version of IAR... versus VS2005... which is not up to date.
– Jonathan Mee
Nov 28 '18 at 14:43