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 7671907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:09:52+00:00 2026-05-31T16:09:52+00:00

I have a set of functions that I declare in a header like this:

  • 0

I have a set of functions that I declare in a header like this:

actual_function.hpp

#ifndef ACTUAL_FUNCTION_HPP
#define ACTUAL_FUNCTION_HPP

#include <iostream>

#ifdef CONDITION
#warning Compiling version 1
template<typename T>
T fun (T x) {
    std::cout << "Version 1 implementation is called.\n";
    return x + x;
}
#else
#warning Compiling version 2
template<typename T>
T fun (T x) {
    std::cout << "Version 2 implementation is called.\n";
    return 2 * x + 1;
}
#endif

#endif

I am trying to test both versions of the function in one test program. I thought that I could do this with multiple translation units, so I have a file layout like this:

main.cpp:

void test_version_1 ();
void test_version_2 ();
int main () {
    test_version_1 ();
    test_version_2 ();
    return 0;
}

test1.cpp:

#include <cassert>
#include <iostream>
#define CONDITION
#include "actual_function.hpp"
void test_version_1 () {
    std::cout << "Version 1 is called.\n";
    assert (fun (8) == 16);
}

test2.cpp

#include <cassert>
#include <iostream>
#undef CONDITION
#include "actual_function.hpp"
void test_version_2 () {
    std::cout << "Version 2 is called.\n";
    assert (fun (8) == 17);
}

My thought was that this would then give test1.cpp version 1 of fun, and test2.cpp version 2 of fun. The pre-processor output appears to support this thought:

g++ main.cpp test1.cpp test2.cpp
In file included from test1.cpp:4:0:
actual_function.hpp:7:2: warning: #warning Compiling version 1 [-Wcpp]
In file included from test2.cpp:4:0:
actual_function.hpp:14:2: warning: #warning Compiling version 2 [-Wcpp]

However, my guess is that the linker is mixing things up on me. When I run the program, this is what happens:

./a.out 
Version 1 is called.
Version 1 implementation is called.
Version 2 is called.
Version 1 implementation is called.
a.out: test2.cpp:7: void test_version_2(): Assertion `fun (8) == 17' failed.
Aborted (core dumped)

If I rename fun to something else in only one of the definitions, and call that newly named function, everything works as expected, which shows that the correct functions are visible in the correct spots. If I only rename a function at the definition, but do not change the point of call, I get a compiler error test2.cpp:7:2: error: ‘fun’ was not declared in this scope. This makes me think that the linker is overwriting functions because they have the same name and signature.

Is this really what is happening? If so, what is the best solution? My two thoughts are as follows:

1: Have my functions take an extra template argument, so it would be template, and then specialize on true vs. false. In reality, I would probably need something a little more complex than this (perhaps specialize on an int or something), because my real problem has a few more options. If the CONDITION macro is defined, it uses a manual version. If the condition macro is not defined, then it sees if it knows about any compiler intrinsics that do what I do manually, and if so, it uses them, otherwise, it falls back on the manual definitions regardless of the presence of the macro. Some sort of template specialization could work here still, though.

2: Create functions with different names fun_manual and fun_intrinsic, and have fun be a wrapper function that calls them based on their name. I’m not entirely sure how this would work.

My primary concern is that if the compiler does not support the intrinsic version, the intrinsic version cannot be seen by the compiler, or it will give an error.

Are either of my two solutions the best I can do, or is there something better?

  • 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-31T16:09:53+00:00Added an answer on May 31, 2026 at 4:09 pm

    You are violating the One Definition Rule. Essentially the combination of compiler and linker are allowed to treat the different versions of the function as if they were the same.

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

Sidebar

Related Questions

I have a problem with a javascript set of functions that I made. This
I have a try-catch block that iterates over a set of records like this:
I have certain initializing functions that I use to set up audit logging on
I have a set of five functions, that could be called one of five
I have declared a DLL import in my C# program that looks like this:
I have an generic math-method, that operates under a set of functions (with a
I want to make sure that a set of functions have the same signature
I have a function that I set custom key and value to it and
I have the following function that does string splitting: on splitText(aString, delimiter) set retVal
I am adding musicCD information to a set. I have two different functions for

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.