c++ - Undefined Reference in inline function, solved with unary + -


i have strange gcc 4.7 problem c++11 enabled :

when want compile this:

constexpr unsigned int getdim(const int e){         return (e==1)?  a::set::dimension :             (               (e==2)? b::set::dimension :                  (                     (e==3)? c::set::dimension :                     (                        +d::set::dimension                      )                 )             );  } 

where each struct a,b,c,d typedef set defined related set has int dimension, example

struct setone{    static const int dimension = 1; }  struct a{    typedef setone set; } 

if don't use unary + infront of d::set::dimension linker fails complaining undefined references setone::dimension.

is same problem as: undefined reference static class member

i cannot give mwe problem vanished simple example 1 .cpp file. ? (but definitions a,b,c,d in 1 header file)

does have clue might go wrong here? unintuitiv :-)

observation 2: if 1 replaces: +d::set::dimension 0, compiles fine, why hack other statements a::set::dimension not rise same linking error?

in expression building, ternary expression yielding lvalue, causes odr-use of static constant. 1 definition rule requires static members odr-used defined, need provide definition (in single translation unit).

so why problem go away unary +?

the unary + not cause odr-use of static member, requires rvalue, , result of rvalue. cascades out through conditional operators once 1 of 2 arguments rvalue result of expression rvalue. end result single + has effect of forcing lvalue-to-rvalue conversion of of static consts used in function , removes odr-uses.

if 1 replaces: +d::set::dimension 0, compiles fine

again, 0 rvalue, , have same effect unary + described above.


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -