Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>UPDATE: <a href="http://blogs.msdn.com/b/ericlippert/archive/2010/06/10/don-t-repeat-yourself-consts-are-already-static.aspx" rel="noreferrer">This question was the subject of my blog on June 10th, 2010.</a> Thanks for the great question!</p> <blockquote> <p>why was the decision made to not force constants to use the static modifier if they are considered static?</p> </blockquote> <p>Suppose constants are considered to be static. There are three possible choices:</p> <ol> <li><p>Make static <strong>optional</strong>: "const int x..." or "static const int x..." are both legal.</p></li> <li><p>Make static <strong>required</strong>: "const int x..." is illegal, "static const int x..." is legal</p></li> <li><p>Make static <strong>illegal</strong>: "const int x..." is legal, "static const int x..." is illegal.</p></li> </ol> <p>Your question is why did we choose (3)?</p> <p>The design notes from 1999 do not say; I just checked. But we can deduce what was probably going through the language designer's heads. </p> <p>The problem with (1) is that you could read code that uses both "const int x..." and "static const int y..." and then you would naturally ask yourself "what's the difference?" Since the default for non-constant fields and methods is "instance" unless "static", the natural conclusion would be that some constants are per-instance and some are per-type, and that conclusion would be wrong. This is bad because it is misleading.</p> <p>The problem with (2) is that first off, it is redundant. It's just more typing without adding clarity or expressiveness to the language. And second, I don't know about you, but I personally hate it when the compiler gives me the error "You forgot to say the magic word right here. I know you forgot to say the magic word, I am one hundred percent capable of figuring out that the magic word needs to go there, but I'm not going to let you get any work done until you say the magic word". </p> <p>The problem with (3) is that the developer is required to know that const logically implies static. However, once the developer learns this fact, they've learned it. It's not like this is a complex idea that is hard to figure out.</p> <p>The solution which presents the fewest problems and costs to the end user is (3).</p> <p>It is interesting to compare and contrast this with other places in the language where different decisions were made. </p> <p>For example, overloaded operators are required to be both public and static. In this case, again we are faced with three options: </p> <ol> <li><p>make public static optional, </p></li> <li><p>make it required, or </p></li> <li><p>make it illegal. </p></li> </ol> <p>For overloaded operators we chose (2). Since the natural state of a method is private/instance it seems bizarre and misleading to make something that looks like a method public/static invisibly, as (1) and (3) both require.</p> <p>For another example, a virtual method with the same signature as a virtual method in a base class is supposed to have either "new" or "override" on it. Again, three choices. </p> <ol> <li><p>make it optional: you can say new, or override, or nothing at all, in which case we default to new. </p></li> <li><p>make it required: you have to say new or override, or </p></li> <li><p>make it illegal: you cannot say new at all, so if you don't say override then it is automatically new.</p></li> </ol> <p>In this case we chose (1) because that works best for the brittle base class situation of someone adds a virtual method to a base class that you don't realize you are now overriding. This produces a warning, but not an error.</p> <p>My point is that each of these situations has to be considered on a case-by-case basis. There's not much general guidance here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload