1

I am trying to create a template class with a boost.bimap as a member. However, when following the usual typedef protocols, my compiler (I'm using Visual Studio Express 2012) produces a whole ream of C4512 (assignment operator could not be generated) warnings. Strangely enough, the code will compile, and if I fully implement the class, things work correctly. I'd prefer to know the cause of the warning though, and how to avoid it, if possible. If anyone had any ideas, I'd be very grateful!

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include <map>
#include <boost/bimap.hpp>

template<typename T>
class TestClass
{
public:
    TestClass()
    {

    }
private:
    typedef boost::bimap<int,int> bimap_t;
    typedef bimap_t::value_type valuetype;
};

#endif // TESTCLASS_H

The bimap code, outside of a template, doesn't cause any warnings to appear.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
DaveM
  • 205
  • 1
  • 9
  • 1
    I suppose the "class" before the constructor is typo. – Johny Sep 20 '13 at 13:23
  • Yes, it was indeed. I've updated the code snippet. – DaveM Sep 20 '13 at 13:27
  • It sounds like something is non-copyable. But from the code you gave there are no members that could be non-copyable members. Are their members of the class your not showing? – andre Sep 20 '13 at 13:28
  • You said you have bimap as a member - I see only two typedefs. – Johny Sep 20 '13 at 13:29
  • Hi Johny, this is the minimum amount of code that will cause the warnings to be thrown. Carrying on with creating the members and adding the desired functionality doesn't cause any other warnings. #include is redundant also. – DaveM Sep 20 '13 at 13:34

1 Answers1

0

From the MSDN documentation

You can resolve the C4512 warning for your code in one of three ways:

  • Explicitly define an assignment operator for the class.
  • Remove const or the reference operator from the data item in the class.
  • Use the #pragma warning statement to suppress the warning.

If inheriting from boost::noncopyable (which would be the first option) does not work, and you cannot access the class source (second option) then you are left with the #pragma warning

#pragma warning( disable : 4152 )
// your offending code
#pragma warning( pop ) 
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • Shouldn't there be a `#pragma warning(push)` before that? – Xeo Sep 21 '13 at 09:33
  • @Xeo `pop()` restores every disabled warning, regardless how they were disabled, see the 2nd link in my answer. – TemplateRex Sep 21 '13 at 10:14
  • Hi Template Rex - I think this may be the most sensible option. I was trying to avoid disabling warnings where possible, but I guess the functionality is there for a reason. I'll have another look at the code today and see if I can figure something out, but if not, I'll go with this. Thanks! – DaveM Sep 23 '13 at 08:17
  • 1
    @DaveM glad to have been of help! BTW, this `#pragma` trick is mentioned in Item 1 of [C++ Coding Standards](http://www.amazon.com/Coding-Standards-Rules-Guidelines-Practices/dp/0321113586), by Alexandrescu & Sutter, which is titled "Compile cleanly at high warning levels". Of course, `-Wall` or `/W4` is best, but failing that, you want to silence all warnings that you know are innocent, but as locally as possible. `#pragma` helps you do just that. – TemplateRex Sep 23 '13 at 08:20