Operating System - HP-UX
1826291 Members
4666 Online
109692 Solutions
New Discussion

Re: static const as a function argument

 
SOLVED
Go to solution
Alex Vinokur
Frequent Advisor

static const as a function argument


Hi,

Why does Foo::foo6() have a problem? (See below)

Thanks.

// =================================
HP-UX B.11.23 U ia64 1139467043 unlimited-user license

aCC: HP C/aC++ B3910B A.06.25.01 [May 16 2010]

92453-07 linker nm HP Itanium(R) B.12.54


// -----------
// Compilation
// -----------
> aCC +DD64 -AA a.cpp
ld: Unsatisfied symbol "Foo::STATIC_CONST_VALUE" in file a.o
1 errors.



// ===== File a.cpp : BEGIN =====
struct Foo
{
enum { ENUM_VALUE_VALUE = 121 };
const int CONST_VALUE;
static int STATIC_VALUE;
static const int STATIC_CONST_VALUE = 127;

Foo();
void foo (const int& i_val);

void foo1 ();
void foo2 ();
void foo3 ();
void foo4 ();
void foo5 ();
void foo6 ();

};

int Foo::STATIC_VALUE = 125;

Foo::Foo() : CONST_VALUE (123)
{
}

void Foo::foo (const int& i_val)
{
}

void Foo::foo1()
{
foo(119);
}

void Foo::foo2 ()
{
foo(ENUM_VALUE_VALUE);
}

void Foo::foo3 ()
{
foo(CONST_VALUE);
}

void Foo::foo4 ()
{
foo(STATIC_VALUE);
}

void Foo::foo5 ()
{
const int theValue = STATIC_CONST_VALUE;
foo(theValue);
}

void Foo::foo6 ()
{
foo(STATIC_CONST_VALUE); // This line causes linker error
}

int main()
{
return 0;
}

// ===== File a.cpp : END =====


// ===== nm a.o | c++filt : BEGIN =====

Symbols from a.o:

[Index] Value Size Type Bind O Shndx Name

[0] | 0| 0|NOTYP|LOCAL|0| UNDEF|
[16] | 0| 0|SECT |LOCAL|0|.HP.opt_annot|.HP.opt_annot
[17] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[18] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[20] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[21] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[22] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[23] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[24] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[25] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[15] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[19] | 0| 0|SECT |LOCAL|0|.IA_64.unwind_info|.IA_64.unwind_info
[3] | 0| 0|SECT |LOCAL|0|.debug_actual|.debug_actual
[2] | 0| 0|SECT |LOCAL|0|.debug_line|.debug_line
[14] | 0| 0|SECT |LOCAL|0|.debug_procs_abbrev|.debug_procs_abbrev
[4] | 0| 0|SECT |LOCAL|0| .text|.text
[5] | 0| 0|SECT |LOCAL|0| .text|.text
[6] | 0| 0|SECT |LOCAL|0| .text|.text
[11] | 0| 0|SECT |LOCAL|0| .text|.text
[7] | 0| 0|SECT |LOCAL|0| .text|.text
[8] | 0| 0|SECT |LOCAL|0| .text|.text
[9] | 0| 0|SECT |LOCAL|0| .text|.text
[10] | 0| 0|SECT |LOCAL|0| .text|.text
[13] | 0| 0|SECT |LOCAL|0| .text|.text
[12] | 0| 0|SECT |LOCAL|0| .text|.text
[38] | 0| 0|FUNC |GLOB |0| UNDEF|_Unwind_Resume
[36] | 0| 4|OBJT |GLOB |0| .sdata|Foo::STATIC_VALUE
[37] | 0| 0|OBJT |GLOB |0| UNDEF|Foo::STATIC_CONST_VALUE
[29] | 0| 32|FUNC |GLOB |0| .text|Foo::foo(int const&)
[30] | 0| 208|FUNC |GLOB |0| .text|Foo::foo1()
[31] | 0| 208|FUNC |GLOB |0| .text|Foo::foo2()
[32] | 0| 176|FUNC |GLOB |0| .text|Foo::foo3()
[33] | 0| 192|FUNC |GLOB |0| .text|Foo::foo4()
[34] | 0| 208|FUNC |GLOB |0| .text|Foo::foo5()
[35] | 0| 192|FUNC |GLOB |0| .text|Foo::foo6()
[27] | 0| 32|FUNC |GLOB |0| .text|Foo::Foo()(complete)
[28] | 0| 176|FUNC |GLOB |0| .text|Foo::Foo()(base)
[1] | 0| 0|FILE |LOCAL|0| ABS|a.cpp
[26] | 0| 16|FUNC |GLOB |0| .text|main


// ===== nm a.o | c++filt : END =====
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: static const as a function argument

>Why does Foo::foo6() have a problem?

Because you violated the ODR rule. You need to have a definition (one) for it.

The current Standard says you must also define Foo::STATIC_CONST_VALUE outside the class:
const int Foo::STATIC_CONST_VALUE;
9.4.2(4)

This is needed for your case where you are taking the address of the variable, to bind to the const ref parm of foo. (Or the compiler is too dumb to do simple constant propagation.)

The next Standard doesn't require the definition outside the class but I assume it says it is still needed if you take the address?
Alex Vinokur
Frequent Advisor

Re: static const as a function argument

Dennis, thank you.