Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 32,648 of 33,346    |
|    SG to All    |
|    Re: Is this a violation of the ODR?    |
|    13 Nov 12 16:01:10    |
   
   From: sgesemann@this.is.invalid   
      
   Am 13.11.2012 08:14, schrieb Matt Calabrese:   
   > While working on a library, I unfortunately need to dance on the edge   
   > of ODR violations. Through several little tricks, I believe I may be   
   > safe, but I'm not certain. Before anyone asks, this sounds like an   
   > "XY problem," but you're going to just have to trust me that I need   
   > to do something hairy like this, and details for why are too   
   > complicated to get into here. Anyway, consider the following example,   
   > obviously reduced from what I'm actually doing:   
   >   
   > ////////////////////   
   > // namespace { bool foo(); }   
   > // inline void bar() { decltype( foo() ) foobar; }   
   > ////////////////////   
   >   
   > So, if the above code is inside of a header file that is included by   
   > two or more different translation units, is the ODR technically still   
   > violated? In other words, if I'm only doing something different in an   
   > unevaluated context, but the resultant type is always the same in   
   > every translation unit, is what I'm doing technically okay? Should I   
   > worry about this even if it is questionable whether or not this is   
   > valid? I don't see how this could possibly cause a problem, but for   
   > the sake of being pedantic, I'd like to know if I'm committing one of   
   > the worst of all C++ sins.   
      
   Quoting N3337.pdf (first draft after standardization, §3.2/5):   
      
    There can be more than one definition of a [...] inline function   
    with external linkage [...] in a program provided that each   
    definition appears in a different translation unit, and provided   
    the definitions satisfy the following requirements. Given such   
    an entity named D defined in more than one translation unit,   
    then   
      
    - each definition of D shall consist of the same sequence of   
    tokens   
      
    - in each definition of D, corresponding names, looked up   
    according to 3.4, shall refer to an entity defined within the   
    definition of D, or shall refer to the same entity, after   
    overload resolution and after matching of partial template   
    specialization, except that a name can refer to a const object   
    with internal or no linkage if the object has the same literal   
    type in all definitions of D, and the object is initialized   
    with a constant expression, and the value (but not the   
    address) of the object is used, and the object has the same   
    value in all definitions of D; and   
      
    - ...   
      
   I think you're violating the 2nd bullet point because the corresponding   
   names "foo" across the translation units don't refer to the same entity   
   since each translation unit has its own foo. On the other hand you're   
   just using its return type which is is the same across all translation   
   units. So, I guess that you won't run into any troubles. But still, it   
   seems like an ODR violation to me, strictly speaking.   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca