title: C++ equivalent of Java static final String
body: <p>I often need to define string constants associated with a class. Examples would be XML attributes, default filenames and many more.</p>
  
  <p>In Java I would simply define something like</p>
  
  <pre><code>public static final String KEY = "attribute";
  </code></pre>
  
  <p>inside the class or interface.</p>
  
  <p>In C++ the following would not compile:</p>
  
  <pre><code>class Example {
  public:
      static const std::string KEY = "attribute";
  }
  </code></pre>
  
  <p>Instead, I would have to write:</p>
  
  <pre><code>class Example {
  public:
      static const std::string KEY;
  }
  
  const std::string Example::KEY = "attribute";
  </code></pre>
  
  <p>This is something I absolutely want to avoid because of its redundancy and verbosity.</p>
  
  <p>The best solution I found so far is to use functions:</p>
  
  <pre><code>class Example {
  public:
      static std::string KEY() const { return "attribute"; }
  }
  </code></pre>
  
  <p>This also provides some encapsulation and flexibility. However it might seem a bit weird to use a function just to define a constant.</p>
  
  <p>So my question is, does this solution have major drawbacks and if yes, what are better alternatives?</p>