| November 15, 2008 11:00 PM PST | |
Cause:
The initializer list with constructor is the only way you can initialize the constant members of a class in C++."const" must already be defined before any user-written code is executed ( including the constructor) and with initializer list you can achieve this.
Code which do not define constructor for initializing const member generates this warning:
struct mystruct {
const int CONST;
};
>icl /c test.cpp
Intel(R) C++ Compiler Professional for applications running on IA-32, Version 11
.0 Build 20080930 Package ID: w_cproc_p_11.0.066
Copyright (C) 1985-2008 Intel Corporation. All rights reserved.test.cpp
test.cpp(1): warning #411: class "mystruct" defines no constructor to initialize
the following:
const member "mystruct::CONST"
struct mystruct {
^
Default Type: "warning"
Compiler generates a similar warning( #409 - http://software.intel.com/en-us/articles/cdiag409 ) when class having const member provides no initializer.
Resolution:
- Provide a constructor with initializer list that initializes the const member of the class
This article applies to: ISN General, Intel® C++ Compiler for Linux* Knowledge Base, Intel® C++ Compiler for Mac OS X* Knowledge Base, Intel® C++ Compiler for Windows* Knowledge Base, Intel® Parallel Composer Knowledge Base
For more complete information about compiler optimizations, see our Optimization Notice.
Comments (1) 
Trackbacks (0)
Leave a comment 
Grishma Kotecha (Intel)
| ||
Jennifer Jiang (Intel)
|


Christian Brolin
This is wrong. It is possible to initialize the constant member in your example with:
mystruct S = { 123 };
Please fix the compiler by removing this warning.