MantisBT - ITK
View Issue Details
0011158ITKpublic2010-08-23 09:012011-06-26 20:46
Hans Johnson 
Hans Johnson 
normalminorhave not tried
assignedopen 
ITK-4-A2 
 
0011158: Explicit instantiation and itkStaticConstMacro are not properly implemented
The itkStaticConstMacro does not appear to create static member functions in a way that is compliant with explicit instantiation.

It also appears that in all cases, the itkStaticConstMacro is only used on types that can also be enumerations. Furthermore, previous 3.20 and earlier releases ALWAYS used enumerations instead of "static const unsigned int", and this is why explicit instantiations worked previously.

A review of Explicit instantiation needs to occur to determine what the proper strategy is.
No tags attached.
Issue History
2010-08-23 09:01Hans JohnsonNew Issue
2010-08-23 09:27Hans JohnsonNote Added: 0021892
2010-08-23 18:35Hans JohnsonNote Added: 0021906
2010-08-23 18:40Hans JohnsonNote Added: 0021907
2010-08-23 19:56Bradley LowekampNote Added: 0021908
2010-08-23 19:57Bradley LowekampNote Edited: 0021908
2010-08-23 19:59Bradley LowekampNote Deleted: 0021908
2010-08-24 07:11Bradley LowekampNote Added: 0021915
2010-08-24 14:32Brad KingNote Added: 0021929
2010-10-12 22:21Hans JohnsonStatusnew => assigned
2010-10-12 22:21Hans JohnsonAssigned To => Hans Johnson
2011-06-26 20:46Hans JohnsonNote Added: 0026980

Notes
(0021892)
Hans Johnson   
2010-08-23 09:27   
It appears that initialization of templated static const variables is a feature that is new in C++0x with the new constexpr keyword.


Intiailization of static const variables still apears to be necessary OUTSIDE the class definition.

All examples referencing this class of problem indicate a solution similar to:

http://www.velocityreviews.com/forums/t458125-template-class-and-static-member-variables.html [^]
(0021906)
Hans Johnson   
2010-08-23 18:35   
Further code review indicates that itkStaticConstMacro is used only for "unsigned int" and a few "bool" types.

Using the non-enumeration variant of itkStaticConstMacro requires separate declaration and initialization of the variables for explicit instantiation to work properly.

http://stackoverflow.com/questions/1563897/c-static-constant-string-class-member [^]

====
Bjarne Stroustrup's C++ Style and Technique FAQ
From : http://www2.research.att.com/~bs/bs_faq2.html [^]
"How do I define an in-class constant?"
(0021907)
Hans Johnson   
2010-08-23 18:40   
You can reference the static const variables in other compilation units (i.e. in the explicitly instantiated units) if (and only if) it has an out-of-class definition.

The "initialization" occurs inside the class, but the definition of the variables existence MUST occur outside the class.

template <class PixelType, unsigned int Dimension> const unsigned int itk::Image<PixelType,Dimension>::ImageDimension;
(0021915)
Bradley Lowekamp   
2010-08-24 07:11   
Only alternative I can think of is inline static member functions.
(0021929)
Brad King   
2010-08-24 14:32   
From C++98 9.4.2/4:

"If a static data member is of const integral or const enumeration
 type, its declaration in the class definition can specify a
 constant-initializer which shall be an integral constant
 expression (5.19). In that case, the member can appear in
 integral constant expressions within its scope. The member shall
 still be defined in a namespace scope if it is used in the
 program and the namespace scope definition shall not contain an
 initializer."

This means that if the member is used only as an "integral constant expression" then it does not need a definition because it is used only at compile time. However, if it is "used in the program" then it needs a definition. This is the case for example when the address of the member is taken. On Windows the dllexport caused by explicit instantiation may also require the definition.

Can you produce a minimal example (separate from ITK) that shows this?
(0026980)
Hans Johnson   
2011-06-26 20:46   
This works as is, but may cause problems for explicit instantiation. Since explicit instantiation is not currently being worked on, this will likely not raise high enough to warrant further investigation.