1

I have the following function in one of my utility header files.

template<typename T>
static void rtrim(std::basic_string<T, std::char_traits<T>, std::allocator<T>> &t) 
{ 
      t.erase(find_if(t.rbegin(), t.rend(), 
              [](T& c)->bool{ return !isspace(c); }).base(), t.end());
} 

I am building the code with Visual Studio 2012 with pre-compiled headers on (/Yu). The build fails with the following error.

1>stdafx.obj : error LNK2005: "public: void __cdecl ::operator()(class std::basic_string,class std::allocator > const &)const " (??R@@QEBAXAEBV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z) already defined in

If I remove /Yu flag, it builds fine. Does it mean lambdas cannot be used with precompiled headers? Is there a work around?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sreeram
  • 21
  • 2
  • Instead of `static void rtrim(...)` try `inline void rtrim(...)`. Does that fix the problem? – Praetorian Jun 07 '14 at 00:26
  • Another option is to get rid of the lambda - `find_if(t.rbegin(), t.rend(), ::isspace)` – Praetorian Jun 07 '14 at 00:34
  • Inlining did not work. My code is full of templates. I want to simplify my template code with c++ lamdas. This is the simple example i am trying. My code was not using lamdas before and it looked exactly like you said. – sreeram Jun 07 '14 at 01:11
  • I noticed that it only happens when native c++11 libraries are linked to managed c++ application. I mean, build errors appear in managed c++ application. – sreeram Jun 07 '14 at 01:19
  • @Praetorian: template functions are already `inline` --well, not strictly speaking, but in reality the properties that `inline` confers to a function are already properties of the function template. – David Rodríguez - dribeas Jun 07 '14 at 02:29
  • @David I think I know what you're saying; it's the whole *marked as weak symbol, linker picks one* thing. sreeram: I tried creating a new project using precompiled headers, and containing 2 cpp files. I included `` and `` in stdafx.h along with the function you have. I then called it in both cpp files, but it doesn't result in any such error. – Praetorian Jun 08 '14 at 06:19

0 Answers0