Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping nested templated types in nim

Tags:

c++

nim-lang

I have a C++ type like:

template <typename T>
class Vector {
  struct Iterator {
  };
};

And in C++ I can use Iterator as Vector<int>::Iterator.

How do I wrap this to use it from Nim? c2nim emits

type Vector[T] {.importcpp...} = object
  type Iterator[T] {.importcpp...}

which doesn't compile because nim doesn't have nested types, and would produce Vector<T>::Iterator<T> rather than Vector<T>::Iterator.

I can use non-nested types in Nim:

type VectorIterator[T] {.importcpp: "Vector::Iterator".}
var v : VectorIterator[cint]

And this naturally produces Vector::Iterator<int>, which is wrong (it should be Vector<int>::Iterator).

Is there a way to change the import specification to produce the correct output?

like image 758
dhasenan Avatar asked Apr 08 '15 23:04

dhasenan


1 Answers

I've recently added the support for wrapping such nested types in the compiler. You'll need to use the latest code from the devel branch. Here is how it can be done:

{.emit: """

template <class T>
struct Vector {
  struct Iterator {};
};

""".}

type
  Vector {.importcpp: "Vector".} [T] = object
  VectorIterator {.importcpp: "Vector<'0>::Iterator".} [T] = object

var it: VectorIterator[int]

The relevant details in the manual can be found here and here.

like image 52
zah Avatar answered Oct 05 '22 03:10

zah