Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 25 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57

The question is more why do you have to explicitly make a static member defined within the class const or inline, when you don't have to do that for a static method, which is automatically made inline when defined within the class. More broadly, the question is why does defining a method in a class make it inline and why can't defining a static methodmember in a class be treated as either inline or like a file scope class scope global symbol like an out-of-line definition, instead of making it a compiler error.

The question is more why do you have to explicitly make a static member const or inline, when you don't have to do that for a static method, which is automatically made inline. More broadly, the question is why does defining a method in a class make it inline and why can't defining a static method in a class be treated as either inline or like a file scope class scope global symbol like an out-of-line definition, instead of making it a compiler error.

The question is more why do you have to explicitly make a static member defined within the class const or inline, when you don't have to do that for a static method, which is automatically made inline when defined within the class. More broadly, the question is why does defining a method in a class make it inline and why can't defining a static member in a class be treated as either inline or like a file scope class scope global symbol like an out-of-line definition, instead of making it a compiler error.

added 106 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57

The only difference it has to a method is that the same definition can have multiple different values -- if you defined the class type in an anonymous namespace it would have static (local) linkagedefined the class type in an anonymous namespace it would have static (local) linkage and then you could avoid the redefinition errors. Now, it would have multiple different states in the different translation units if it were included into the translation units but a method would not (it would have multiple definitions but those definitions would be identical). The same is true with non static linkage, where one of the definitions will be arbitrarily selected if the method is inline, otherwise there will be a multiple definition error. With static linkage, it also still prevents you from defining a static member in the class without inline, because it still would not emit a symbol.

The only difference it has to a method is that the same definition can have multiple different values -- if you defined the class type in an anonymous namespace it would have static (local) linkage and then you could avoid the redefinition errors. Now, it would have multiple different states in the different translation units if it were included into the translation units but a method would not (it would have multiple definitions but those definitions would be identical). The same is true with non static linkage, where one of the definitions will be arbitrarily selected if the method is inline, otherwise there will be a multiple definition error. With static linkage, it also still prevents you from defining a static member in the class without inline, because it still would not emit a symbol.

The only difference it has to a method is that the same definition can have multiple different values -- if you defined the class type in an anonymous namespace it would have static (local) linkage and then you could avoid the redefinition errors. Now, it would have multiple different states in the different translation units if it were included into the translation units but a method would not (it would have multiple definitions but those definitions would be identical). The same is true with non static linkage, where one of the definitions will be arbitrarily selected if the method is inline, otherwise there will be a multiple definition error. With static linkage, it also still prevents you from defining a static member in the class without inline, because it still would not emit a symbol.

added 135 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57

So you have to ask why the intention of a member defined in a class would be out-of-line (i.e. like a regular file scope symbol), and the intention of a method defined in a class be inline (i.e. emit symbol only if it is used in the translation unit, and select only one symbol in the comdat group to useselect only one symbol in the comdat group to use). I mean, when a programmer defines a static method in a class, it's assumed to be the same method, but when they define a member, it could have many different values, which is an issue for comdat linkage, because you don't know which symbol it is going to select, and will be a silent logic error:

So you have to ask why the intention of a member defined in a class would be out-of-line (i.e. like a regular file scope symbol), and the intention of a method defined in a class be inline (i.e. emit symbol only if it is used in the translation unit, and select only one symbol in the comdat group to use). I mean, when a programmer defines a static method in a class, it's assumed to be the same method, but when they define a member, it could have many different values, which is an issue for comdat linkage, because you don't know which symbol it is going to select, and will be a silent logic error:

So you have to ask why the intention of a member defined in a class would be out-of-line (i.e. like a regular file scope symbol), and the intention of a method defined in a class be inline (i.e. emit symbol only if it is used in the translation unit, and select only one symbol in the comdat group to use). I mean, when a programmer defines a static method in a class, it's assumed to be the same method, but when they define a member, it could have many different values, which is an issue for comdat linkage, because you don't know which symbol it is going to select, and will be a silent logic error:

deleted 611 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57
Loading
added 125 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57
Loading
added 290 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57
Loading
added 358 characters in body
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57
Loading
Source Link
Lewis Kelsey
  • 4.8k
  • 1
  • 42
  • 57
Loading