Reference Initialization

ntuple<> can be instantiated with reference (T &) and constant reference (T const &) template arguments, however ntuple<>'s constructor takes its arguments by const &, so a wrapper type [1] is needed, imp::ref< T >() return's such a type.

int i = 10;
double const d = 2.3;
ntuple< int &, double const & > id( ref( i ), d );

std::cout << id << '\n';

CHECK( id.get< 1 >() == 2.3 );

id.set< 0 >( 4 );

CHECK( i == 4 && d == 2.3 );

std::cout << id << '\n';

Test Result: gcc34 Passed, msvc80 Passed, msvc71 Passed

Output

(10,2.3)
(4,2.3)

Warning

The constructor invocation in the above:

ntuple< int &, double const & > id( ref( i ), d );

could succesfully (no diagnostic required) be replaced by:

ntuple< int &, double const & > id( ref( i ), 2.3 );

However the resuilting programme will exhibit Undefined Behavior, as the second element will refer to a temporary that will be destroyed after the constructor completes.

[1]

The wrapper type needs to provide a convertion to template_paramiter_reference_type.

Any type that provides an implicit conversion from argument_type const to template_paramiter_reference_type will do.

  • template_paramiter_reference_type is int & in the above example.