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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:19:35+00:00 2026-06-05T08:19:35+00:00

In my code, I have got function templates containing lambda expressions which depend on

  • 0

In my code, I have got function templates containing lambda expressions which depend on some of the template parameters. Recently I got linker errors, maybe due to an update of my g++-compiler, but unfortunately, I do not know exactly.

I will give a small example that demonstrates the issue. Because it is a linker issue, we have to create a couple of files to demonstrate it. We have common.hpp, which contains a common template function, two modules a.cpp/a.hpp and b.cpp/b.hpp making use of that function and a main.cpp module containing the main function.

// common.hpp
#include <algorithm>

template <class Iterator, typename Iterator::value_type x>
void
my_transform(Iterator begin, Iterator end)
{
  std::transform(begin, end, begin,
                 [] (typename Iterator::value_type y) { return x+y; });
}

File a.cpp:

// a.cpp
#include "common.hpp"
#include "a.hpp"

void a(std::vector<int>& vec)
{
  my_transform<std::vector<int>::iterator, 5>(vec.begin(), vec.end());
}

File a.hpp

#include <vector>
void a(std::vector<int>& vec);

File b.cpp:

// b.cpp
#include "common.hpp"
#include "b.hpp"

void b(std::vector<int>& vec)
{
  my_transform<std::vector<int>::iterator, 5>(vec.begin(), vec.end());
}

File b.hpp

#include <vector>
void b(std::vector<int>& vec);

File main.cpp

int main() { return 0; }

If I compile and link using

g++-4.7 -std=c++11 -c a.cpp
g++-4.7 -std=c++11 -c b.cpp
g++-4.7 -std=c++11 -c main.cpp
g++ a.o b.o main.o

I get a multiple-definition error:

b.cpp:(.text+0x30): multiple definition of `void my_transform<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, 17>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >)::{lambda(int)#1}::operator int (*)(int)() const'
a.o:a.cpp:(.text+0x30): first defined here

Basically it says that the lambda expression was already defined in a. Okay. If I change the template parameter in b from 5 to 7, everything works.

Questions:

  1. Is this what I should expect or is it a bug in g++? I am very sure that I compiled this code with an earlier version of the debian package of g++-4.7.
  2. Are there any workarounds apart from not using lambdas? If the resulting symbols were static there wouldn’t be any problems for example, I think. Update: Workaround: Make my_transform static or inline.

This question is not very important. There is no problem with the “do not use lambdas here” approach, but I am curious. 🙂

  • 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-05T08:19:36+00:00Added an answer on June 5, 2026 at 8:19 am

    It is a bug in g++, which was introduced between 4.7.0-11 and 4.7.0-12 (I tested those two Debian versions). gcc-snapshot (20120601-1) is fine, too, unfortunately, I don’t know what are the differences between those too – 4.7.0-12 is 6 days later and a different branch, and I don’t have a gcc repository here to compare). I couldn’t find the relevant entry in gcc’s bugzilla.

    The relevant section of the standard is

    There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with
    external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member
    of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for
    which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition
    appears in a different translation unit, and provided the definitions satisfy the following requirements … then the program shall behave as if there were a single definition of D [D is that class/function/whatever].

    When you apply this paragraph to my_transform, you see that it is a non-static function template, it satisfies the requirements (omitted for brevity), so the program should behave as if there was only one definition in the whole program. That holds regardless of what is inside it, so it doesn’t matter if the operator() of the lambda is or isn’t inline (it should be, but doesn’t matter really1).

    BTW the poor man’s lambda function equivalent (which should really be equivalent AFAIK)

    template <class Iterator, typename Iterator::value_type x>
    void
    my_transform(Iterator begin, Iterator end)
    {
      struct Foo {
        auto operator()(typename Iterator::value_type y) const -> decltype(x+y) { return x+y; }
      };
      std::transform(begin, end, begin,
                     Foo());
    }
    

    still works.

    1: I’m not sure if it itself could be subject to ODR, since it has no linkage (cf. 5.1.2/3 and 3.5/8)

    Just to be sure I don’t forget it, the bug was introduced by commit f899a730d4f41b6a20b5508059a450f3a9347316

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

Sidebar

Related Questions

I have got code like this var challegneListener; $(document).ready(function(){ var challegneListener = setInterval(challengeListenerBot(),5000); });
I have got the following very simple code: function init() { var articleTabs =
I have got the following issue, my function appends code to a string $string
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have got a piece of code, that should countdown some number (in this
I've got a problem with using django's {% trans %} template function. I have
I have one template function which will take a pointer type and i have
Trying to study C++ function templates. As a part of which I have this
I have got the code to find the IMEI number of the device, But
I have got this code and I need to access somehow Button and call

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.