How to pass a general list :: by reference?

In an attempt to wrap some unmanaged code in a managed .dll, I'm trying to convert Generic::List

data points to std::vector

. Here is a snippet of what I am trying to do:

namespace ManagedDLL
{
    public ref class CppClass
    {
        void ListToStdVec( const List<double>& input_list, std::vector<double>& output_vector )
        {
            // Copy the contents of the input list into the vector
            // ...
        }

        void ProcessData( List<double> sampleData )
        {
            std::vector<double> myVec;

            ListToStdVec( sampleData, myVec );

            // Now call the unmanaged code with the new vector
            // ...
        }
    }
}

      

Compiling this gives me:

error C3699: '&': cannot use this indirect expression for type `const System :: Collections :: Generic :: List '

I probably missed something principled here (I'm relatively new to the .net way of doing things), but it looks like sane code to me.?

[Edit] I've tried Andy and Dario's suggestions and they work, but how can I access the members of the input list? I've tried all kinds of dreferencing combinations and nothing compiles:

void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector )
{
    int num_of_elements = input_list->Count;
}

void ListToStdVec( const List<double>^ input_list, std::vector<double>& output_vector )
{
    int num_of_elements = input_list.Count;
}

      

... both give me:

error C2662: 'System :: Collections :: Generic :: List :: Count :: get': cannot convert pointer 'this' from' const System :: Collections :: Generic :: List 'to' System :: Collections: : Generic :: List% '

... so how do you access the link / pointer?

+1


a source to share


2 answers


Because it List<T>

is a .NET managed class, it is passed to the managed GC-Handle denoted by the ^ character, not a C ++ reference.

Example:



void ListToVec(List<double>^ input_list, std::vector<double>& out)

      

You don't need extra here const

. Notation List<T>^%

creates a tracking link (comparable to pointers in C ++), not a call by reference. Just sign in with list->...

and list[...]

.

+1


a source


According to Herb Sutter , %

this is skipping a managed entity over a reference symbol. Convert your code to the following and it should work:

void ListToStdVec( const List<double>% input_list, std::vector<double>& output_vector
{
    // Copy the contents of the input list into the vector
    // ...
}

      



Edit . I think that's const

causing the problem, although I'm not sure why. If you change the argument List

to not be const

, then the first function will compile if you use an operator ->

, and the second function will compile if you use an operator .

(I'm not sure why this difference exists - it doesn't make much sense).

That said, if all you want to do is copy the elements in List

to vector

, then you really want to use ^

. Think of it as a reference to a managed object. I think this %

would be used if you want to pass a link "by reference" (ie, Reassign input_list

to something else in ListToStdVec()

) and have the caller invoke the result of that assignment. However, given that you are using an operator .

to access members when using %

that tells me that I cannot understand the purpose of this at all.

+2


a source







All Articles