Sign in to follow this  
Grumple

C++ Visual Studio std::vector::emplace_back c++11 vs c++17

Recommended Posts

I have a general c++ template programming question...I've tried to setup a class that used a nested template type T, along with variadic template arguments for a constructor of T, to ensure I can add items to a custom container class without any unnecessary heap usage/copy/move operations.  

Here is my test container code, using perfect forwarding and std::vector::emplace_back() to create items 'in-place' with little overhead (TemplateContainerTest.h):

#pragma once

#include <vector>
#include <unordered_map>
#include <utility>

template <typename T>
class TemplateContainerTest
{
public:
	TemplateContainerTest() = default;
	virtual ~TemplateContainerTest() = default;

	template<typename... ItemArgs>
	void AddItem( ItemArgs&&... itemArgs )
	{
		m_Container.emplace_back( std::forward<ItemArgs>( itemArgs )... );
	}
protected:
	template <typename T>
	class ItemTracker 
	{
	public:
		template<typename... ItemArgs >
		ItemTracker( ItemArgs&&... itemArgs ):
			m_Item( std::forward<ItemArgs>( itemArgs )... )
		{
		}

		bool m_IsValid = false;

		T m_Item;
	};

	std::vector< ItemTracker<T> > m_Container;
};

And here is some code to exercise the template above (main.cpp):

#include "stdafx.h"
#include <stdint.h>

#include "CNTemplateContainer.h"

class TestItemOfInterest
{
public:
	TestItemOfInterest( uint32_t itemVal ):
		m_ItemVal( itemVal )
	{
	}

	TestItemOfInterest( TestItemOfInterest && other )
	{
		m_ItemVal = other.m_ItemVal;

		other.m_ItemVal = 0;
	}

	TestItemOfInterest() = default;
	virtual ~TestItemOfInterest() = default;

	uint32_t GetVal() { return m_ItemVal; }
protected:
	uint32_t m_ItemVal = 0;
};


int _tmain(int argc, _TCHAR* argv[])
{
	TemplateContainerTest<TestItemOfInterest> tmpContainer;

	tmpContainer.AddItem( 42 );

	return 0;
}

Here is the kicker: in Visual Studio 2013, the code above fails to compile with the following error:

templatecontainertest.h(28): error C2664: 'TestItemOfInterest::TestItemOfInterest(const TestItemOfInterest &)' : cannot convert argument 1 from 'TemplateContainerTest<TestItemOfInterest>::ItemTracker<T>' to 'uint32_t'

However, in Visual Studio 2017 it compiles fine.   For some reason, the perfect forwarding mechanism of Visual Studio 2013 seems to try to send the 'ItemTracker' into the T() constructor instead of just the arguments from outside.   I see that the VS 2017 std::vector::emplace_back signature/implementation changed, but I can't understand why it works now/didn't work before...

Any insight would be appreciated as I don't trust this at all without understanding the underpinning issues...

Edited by Grumple

Share this post


Link to post
Share on other sites

Might have been a bug in 2013 that was fixed for 2015/17.  VS 2013 didn't fully support C++11 features.  I don't even think 2017 is fully compliant yet.  Though they're close from what I read.  Any reason you need to test this on a 4 year old compiler?  Just curious.

Share this post


Link to post
Share on other sites

Yeah that could very well be it, just looking to rule out that it 'shouldn't compile' for some reason.

I have no good excuse for the old compiler other than this being the first issue that has really made me feel the need to change.  Having seen this code compile on a friends VS2017 setup, I will likely be be updating asap...

Thanks!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628360
    • Total Posts
      2982262
  • Similar Content

    • By Kazuma506
      I am trying to recreate the combat system in the game Life is Feudal but make it more complex. The fighting system works by taking in the direction of the mouse movement and if you press the left click it will swing in that direction, though stab, overhead, left (up, down, left right) and right are the only swings that you can do. If you wanted to you could also hold the swing by holding left click so you are able to swing at the perfect moment in the battle. I want to change this so add in more swing directions but I also want to code this from scratch in Unreal. Can anyone give me any pointers or maybe a few snippets of code that work in Unreal that could help me start to implement this type of system?
       
       
    • By rXpSwiss
      Hello,
      I am sending compressed json data from the UE4 client to a C++ server made with boost.
      I am using ZLib to compress and decompress all json but it doesn't work. I am now encoding it in base64 to avoid some issues but that doesn't change a thing.
      I currently stopped trying to send the data and I am writing it in a file from the client and trying to read the file and decompress on the server side.
      When the server is trying to decompress it I get an error from ZLib : zlib error: iostream error
      My question is the following : Did anyone manage to compress and decompress data between a UE4 client and a C++ server ?
      I cannot really configure anything on the server side (because boost has its ZLib compressor) and I don't know what is wrong with the decompression.
      Any idea ?
      rXp
    • By noodleBowl
      I was wondering if someone could explain this to me
      I'm working on using the windows WIC apis to load in textures for DirectX 11. I see that sometimes the WIC Pixel Formats do not directly match a DXGI Format that is used in DirectX. I see that in cases like this the original WIC Pixel Format is converted into a WIC Pixel Format that does directly match a DXGI Format. And doing this conversion is easy, but I do not understand the reason behind 2 of the WIC Pixel Formats that are converted based on Microsoft's guide
      I was wondering if someone could tell me why Microsoft's guide on this topic says that GUID_WICPixelFormat40bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA and why GUID_WICPixelFormat80bppCMYKAlpha should be converted into GUID_WICPixelFormat64bppRGBA
      In one case I would think that: 
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat32bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA, because the black channel (k) values would get readded / "swallowed" into into the CMY channels
      In the second case I would think that:
      GUID_WICPixelFormat40bppCMYKAlpha would convert to GUID_WICPixelFormat64bppRGBA and that GUID_WICPixelFormat80bppCMYKAlpha would convert to GUID_WICPixelFormat128bppRGBA, because the black channel (k) bits would get redistributed amongst the remaining 4 channels (CYMA) and those "new bits" added to those channels would fit in the GUID_WICPixelFormat64bppRGBA and GUID_WICPixelFormat128bppRGBA formats. But also seeing as there is no GUID_WICPixelFormat128bppRGBA format this case is kind of null and void
      I basically do not understand why Microsoft says GUID_WICPixelFormat40bppCMYKAlpha and GUID_WICPixelFormat80bppCMYKAlpha should convert to GUID_WICPixelFormat64bppRGBA in the end
       
    • By HD86
      As far as I know, the size of XMMATRIX must be 64 bytes, which is way too big to be returned by a function. However, DirectXMath functions do return this struct. I suppose this has something to do with the SIMD optimization. Should I return this huge struct from my own functions or should I pass it by a reference or pointer?
      This question will look silly to you if you know how SIMD works, but I don't.
    • By pristondev
      Hey, Im using directx allocate hierarchy from dx9 to use a skinned mesh system.
      one mesh will be only the skeleton with all animations others meshes will be armor, head etc, already skinned with skeleton above. No animation, idle position with skin, thats all I want to use the animation from skeleton to other meshes, so this way I can customize character with different head, armor etc. What I was thinking its copy bone matrices from skeleton mesh to others meshes, but Im a bit confused yet what way I can do this.
       
      Thanks.
  • Popular Now