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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:10:11+00:00 2026-06-06T16:10:11+00:00

Imagine you have a file a.h #include <iostream> template<typename T> struct A{ int magic;

  • 0

Imagine you have a file a.h

 #include <iostream>

template<typename T> struct A{  
  int magic;
  A():magic(1234){}
  void f(){std::cout<<"default f"<<magic<<std::endl;}
};


void f(A<int>* a);

then the function f is defined in “a.cpp”

  #include "a.h"
void f(A<int>* a){
  a->f();
}

and finally, “main.cpp” specializes the template, and then uses f

#include "a.h"
template<> struct A<int>{   
};

int main(){
  A<int> a;
  f(&a);

}

Clearly the compiler uses the non specialized version for a.o, and the specialized version for main.o, i.e. there happen to be two different implementations of A.
On execution, f can only print garbage / segfault, because the object passed has a different structure from the one expected.

Is there a way to make the linker warn that there’re two versions of A ?

  • 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-06-06T16:10:14+00:00Added an answer on June 6, 2026 at 4:10 pm

    The reason Gold doesn’t warn about this is that Gold only detects symbol mismatches (the same symbol being defined in multiple object files in incompatible ways), and there is no such mismatch in the example.

    Running the example under Valgrind does produce this error though:

    valgrind --track-origins=yes ./a.out
    
    ==11004== Memcheck, a memory error detector
    ==11004== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
    ==11004== Using Valgrind-3.8.0.SVN and LibVEX; rerun with -h for copyright info
    ==11004== Command: ./a.out
    ==11004== 
    ==11004== Conditional jump or move depends on uninitialised value(s)
    ==11004==    at 0x40B6D24: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib64/libstdc++.so.6.0.16)
    ==11004==    by 0x40B703C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, long) const (in /usr/lib64/libstdc++.so.6.0.16)
    ==11004==    by 0x40C26DE: std::ostream& std::ostream::_M_insert<long>(long) (in /usr/lib64/libstdc++.so.6.0.16)
    ==11004==    by 0x40094F: A<int>::f() (a.h:6)
    ==11004==    by 0x4008CB: f(A<int>*) (a.cpp:3)
    ==11004==    by 0x400977: main (main.cpp:7)
    ==11004==  Uninitialised value was created by a stack allocation
    ==11004==    at 0x400964: main (main.cpp:5)
    

    You should get even better report from Address Sanitizer:

    Update:

    The point is that I would like to detect the error at linking time, not during execution.

    I understand your point, but it is not currently possible for either the compiler (doesn’t have info about other translation units) or the linker (doesn’t have info about types involved) to warn you about this.

    Now, for a debug build, the linker could in theory do this, if for every function it also compared the debug info for parameter types. I suggest filing a feature request for gold in bugzilla.

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

Sidebar

Related Questions

Imagine you have an object foo that you saved as saved.file.rda as follows: foo
Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY
I have a .ps1 file in which I want to define custom functions. Imagine
Imagine we have a program trying to write to a particular file, but failing.
Imagine I have the following situation: File1.php <?php include(Function.php); log(test); ?> Function.php <?php function
Imagine we have a class like this: class Testee { public: void Func() private:
Imagine I have the folling XML file: <a>before<b>middle</b>after</a> I want to convert it into
Imagine you have a data.xml file: <?xml version=1.0 encoding=UTF-8 ?> <root> <item>value1</item> <item>value2</item> <item>value3</item>
Imagine you have an entity that has some relations with other entities and you

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.