C++ Mapping for Constants

Slice constant definitions map to corresponding C++ constant definitions. For example:

Slice
const bool      AppendByDefault = true;
const byte      LowerNibble = 0x0f;
const string    Advice = "Don't Panic!";
const short     TheAnswer = 42;
const double    PI = 3.1416;

enum Fruit { Apple, Pear, Orange };
const Fruit     FavoriteFruit = Pear;

Here are the generated definitions for these constants:

C++
const bool          AppendByDefault = true;
const Ice::Byte     LowerNibble =     15;
const std::string   Advice =          "Don't Panic!";
const Ice::Short    TheAnswer =       42;
const Ice::Double   PI =              3.1416;

enum Fruit { Apple, Pear, Orange };
const Fruit         FavoriteFruit =   Pear;

All constants are initialized directly in the header file, so they are compile-time constants and can be used in contexts where a compile-time constant expression is required, such as to dimension an array or as the case label of a switch statement.

A Slice string literal that contains non-ASCII characters is mapped by default to a narrow C++ string literal with the non-ASCII characters replaced by their UTF-8 encoding in octal escapes. For example:

Slice
const string Egg = "œuf"; 

is mapped to:

C++
const std::string Egg = "\305\223uf";

If you map a string constant to a std::wstring, the non-ASCII characters in the string literal are replaced by universal character names, except for non-ASCII characters in the range \u0000 to \u009f that are replaced by octal escapes with the same value. For example:

Slice
const ["cpp:type:wstring"] string LargeEgg = "gros œuf"; 

is mapped to:

C++
const std::wstring LargeEgg = L"gros \u0153uf";


Likewise, a Slice string literal that contains universal character names is mapped to a narrow C++ string with the universal character names replaced by their UTF-8 encoding in octal escapes, or to a wide C++ string with the universal character names preserved (except for characters in the range \u0000 to \u009f that are replaced by octal escapes with the same value). For example:

Slice
const string Heart = "c\u0153ur";
const ["cpp:type:wstring"] string BigHeart = "grand c\u0153ur"; 
const ["cpp:type:wstring"] string Banana = "\U0001F34C";
 
const string DoubleTilde = "~\u007e";
const ["cpp:type:wstring"] string WDoubleTilde = "~\u007e";

is mapped to:

C++
const std::string Heart = "c\305\223ur";
const std::wstring BigHeart = L"grand c\u0153ur";
const std::wstring Banana = L"\U0001F34C";
 
const std::string DoubleTilde = "~\176";
const std::string WDoubleTilde = L"~\176";

See Also