Template support and collections
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Domain
- backend-api-design, tooling
Research direction
Start by reviewing the linked CppSharp issues 836 and 1201, then reproduce the report with simple_vector.h and simple_vector.cpp under the stated MSVC settings. Inspect why the templated simple_vector is absent from generated C# and how array parameters and returns are represented. Done means template classes and collection/array interop are supported with clear generated C# output.
Written by the indexing model from the issue text.
Description
Brief Description
Presently there is not way, as far as I can tell, to pass arrays, vectors, or anything templated to between C# and C++.
According to this issue, template support has been completed, but it was speaking to the future. Are templates supported?
https://github.com/mono/CppSharp/issues/836
I saw this: https://github.com/mono/CppSharp/issues/1201
However, none of my C++ template classes are being represented on the C# side. Classes like unique_ptr are getting members generated, but ultimately are being treated as simply ptr fields. That's fine for my purposes, but the following class does not get a corresponding class in C#. In the example below, the public header is all that is being processed by CppSharp, and the pimpl implementation hiding pattern works fine everywhere else. However there is no SimpleVector class or simple_vector class on the C# side. I am even adding an explicit template instantiation which I tried to put in both the cpp file and the header.
simple_vector.h
#pragma once
#pragma warning( disable : 4251 )
#include <memory>
#include <mapping/export.h>
#include <mapping/spimpl.h>
namespace mapping
{
template<typename T>
class MAPPING_API simple_vector
{
public:
simple_vector();
private:
class Impl;
spimpl::unique_impl_ptr<Impl> _pImpl;
};
template MAPPING_API class simple_vector<int>;
}
simple_vector.cpp
#include <mapping/simple_vector.h>
#include <vector>
namespace mapping
{
template<class T>
class simple_vector<T>::Impl
{
std::vector<T> _vector;
public:
Impl() {}
};
template<class T>
simple_vector<T>::simple_vector() : _pImpl(spimpl::make_unique_impl<Impl>()) {}
template MAPPING_API class simple_vector<int>;
}
Furthermore, when trying to pass an array from C# to C++, it will always require an additional parameter for the length of the array. I also cannot figure out how to return an array from C++ to C# in anyway whatsoever. The best I can think to do is to create a pimpl style C++ class for every possible array type I'll be using - IntArray, DoubleArray, FooClassArray, etc. Yes, I could use macros on the C++ side to make things a little less painful, but it's still rather unwieldy.
OS: Windows
Used headers
Used settings
Target: MSVC
Other settings
Stack trace or incompilable generated code
- Dominant language
- C#
- Stars
- 3.4k
- Forks
- 541
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mono/CppSharp
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
How to get started Open
Difficulty 3/5 1-2 days Newbie friendliness 38/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100