locked
Can't compile WinRT C++ / CX Component if you use Vector of value struct

    Question

  • The following code will not compile (error C2678):

       public value struct MyStruct
       {
          int x;
       };
    
       public ref class MyClass sealed
       {
          private: IVector<MyStruct>^ _structs;
          public: MyClass()
          {
             _structs = ref new Vector<MyStruct>();
          };
          public: static void ConvertStruct(IntPtr src, int count, IVector<MyStruct>^ dest);
       };

    Friday, May 18, 2012 8:05 PM

Answers

  • struct MyEqual : public std::binary_function<const MyStruct, const MyStruct, bool> 
    {
        bool operator()( const MyStruct& _Left,  const MyStruct& _Right ) const
        {
    	return _Left.x == _Right.x;
        };
    };
    
    // usage
    
    IVector<MyStruct>^ _structs = ref new Vector<MyStruct, MyEqual>();
    
    for(int i=0; i<10; i++)
    {
         MyStruct m;
         m.x = i;
         _structs->Append(m);
    }
    
    unsigned int index;
    MyStruct m2;
    m2.x = 5;
    _structs->IndexOf(m2,&index);

    • Marked as answer by BullyOwner Tuesday, August 14, 2012 1:55 AM
    Wednesday, August 01, 2012 11:04 PM
    Moderator

All replies

  • Yeah, reproduced - seems like a bug (I think this was present in the DP too).

    http://blog.voidnish.com

    Saturday, May 19, 2012 2:26 PM
  • I wrote collection.h.

    Vectors of value structs will compile with VC11 RTM (as I recall, this was affected by a compiler bug in VC11 Beta), but you will have to provide a comparator: PC::Vector<MyValueStruct, MyEqual>.  This is because WFC::IVector<T>::IndexOf() wants to compare Ts for equality, but value structs don't have op==.  Therefore you must provide a functor.  (It defaults to std::equal_to, which works for ints and hats.)

    By the way, you should always provide the exact text of a compiler error.  Just saying "error C2678" makes it harder to figure out what you're seeing.

    Wednesday, May 30, 2012 1:10 AM
  • how does one write the op== functor?

    Wednesday, August 01, 2012 6:57 PM
  • struct MyEqual : public std::binary_function<const MyStruct, const MyStruct, bool> 
    {
        bool operator()( const MyStruct& _Left,  const MyStruct& _Right ) const
        {
    	return _Left.x == _Right.x;
        };
    };
    
    // usage
    
    IVector<MyStruct>^ _structs = ref new Vector<MyStruct, MyEqual>();
    
    for(int i=0; i<10; i++)
    {
         MyStruct m;
         m.x = i;
         _structs->Append(m);
    }
    
    unsigned int index;
    MyStruct m2;
    m2.x = 5;
    _structs->IndexOf(m2,&index);

    • Marked as answer by BullyOwner Tuesday, August 14, 2012 1:55 AM
    Wednesday, August 01, 2012 11:04 PM
    Moderator
  • I'm trying to use this but I'm getting a compiler warning saying that MyStruct is not a valid generic argument type (using my own struct..)

    Thursday, August 23, 2012 8:50 PM
  • Oops, I guess I missed that it has to be a value struct, and that native types in the struct won't work.

    Thursday, August 23, 2012 8:54 PM