Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6752337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:01:59+00:00 2026-05-26T13:01:59+00:00

Am I violating the One Definition Rule with the following program? // foo.hpp #ifndef

  • 0

Am I violating the One Definition Rule with the following program?

// foo.hpp
#ifndef FOO_HPP_
#define FOO_HPP_

namespace {
   inline int foo() {
       return 1;
   }
}

inline int bar() {
    return foo();
}
#endif
//EOF

and

// m1.cpp

#include "foo.hpp"

int m1() {
    return bar();
}

//EOF

and

// m2.cpp

#include "foo.hpp"

int m2() {
    return bar();
}

//EOF

and finally

// main.cpp
#include <iostream>

int m1();
int m2();

int main(int, const char* [])
{
    int i = m1();
    int j = m2();

    std::cout << (i+j) << std::endl;
    return 0;
}

// EOF

In the above, note that foo() is defined in an anonymous namespace, so I expect that each translation unit m1.cpp and m2.cpp will get its own version, so there is no violation of the ODR. On the other hand, bar() is just a plain old inline function which happens to call 2 different foos. So it violates the ODR, right?

Update:
Previously I had macros in the definition of foo that changed the value it returned and each of m1 and m2 defined the macro differently before including foo.hpp. (And with that previous example, g++ would produce a binary that output (i+j) with a value other than what you would expect.) But in fact this program violates the ODR even if the body of foo() is identical.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:01:59+00:00Added an answer on May 26, 2026 at 1:01 pm

    This does violate the ODR. See 3.2/5 which is talking about extern inline functions (bar):

    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…

    In this case bar refers to two different versions of foo, thus violating the rule.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a pretty trivial one. Can C# return a type that is
ERROR: Failed to enable constraints. One or more rows contain values violating non-null, unique,
I'm getting Failed to enable constraints. One or more rows contain values violating non-null,
How can I find out which column and value is violating the constraint? The
I have seen a codebase recently that I fear is violating alignment constraints. I've
Here's the method. I want to know if I am violating any best practices
I got error Access Violation when the following code is execute : void NoAction()
FxCop is currently reporting the same rule violation for a particular method -- it
My current problem is very similar to this one . I have a downloadFile(URL)
Let's say I have a large collection of Image models and at any one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.