autocxx icon indicating copy to clipboard operation
autocxx copied to clipboard

Templated Class that doesn't use type arguments in body generates incorrect autocxxgen_ffi.h

Open eggonabull opened this issue 2 years ago • 2 comments

Expected Behavior

Correct number of type parameters supplied to templated class implementations

Actual Behavior

error: wrong number of template arguments (n-x, should be n) where n is the number of type parameters the template class expects and x is the number of type parameters not present in the template class body

Steps to Reproduce the Problem

  1. Create a new autocxx project bugdemo.h
#include <string>
#include <vector>
#include <cstring>

#pragma once


namespace BugDemo
{

class Interface {
public:
    struct Implementation {
       virtual ~Implementation() {}
    };
};

template<typename T> class ListBaseImplementation;

class Item;


template<typename L, typename T>
class ListIterator {
public:
  ListIterator<L,T>() : m_list(0), m_index(-1) {}

private:
    const L* m_list;
    int m_index;
};

class ItemList : public Interface {
public:
// For internal use only.
ItemList(const ListBaseImplementation<Item>&);

typedef ListIterator<ItemList, Item> item_iterator;

item_iterator begin() const;

};

}

main.rs

use autocxx;

autocxx::include_cpp! {
    #include "bugdemo.h"
    generate!("BugDemo::ItemList")
    safety!(unsafe_ffi)
}

use ffi as cffi;

fn main() {
    println!("Hello, world!");
    cffi::BugDemo::ItemList
}

build.rs

fn main() -> miette::Result<()> {
    let path = std::path::PathBuf::from("src"); // include path
    let mut b = autocxx_build::Builder::new("src/main.rs", &[&path])
        .build()?;
        // This assumes all your C++ bindings are in main.rs
    b.flag_if_supported("-std=c++17")
     .compile("cxxbugdemo2");
    println!("cargo:rerun-if-changed=src/main.rs");
    Ok(())
}
  1. Attempt to build

Specifications

  • Version: autocxx = "0.22.0"
  • Platform: Ubuntu 20.04 cargo 1.60.0

eggonabull avatar May 17 '22 21:05 eggonabull

Thanks for the report, and thanks very much for taking the time to come up with a minimal code example. It's very much appreciated. There is a reasonable chance that this is a duplicate of #1094. I'll endeavour to fix that first and then see if this problem goes away.

adetaylor avatar May 17 '22 23:05 adetaylor

Thanks @adetaylor.

For those facing this issue, there are two potential workarounds:

  1. You can attempt to comment out the typedef and any class methods that depend on it. In this case, you will you get all the class methods that don't depend on the problematic typedef.
  2. You can add the missing type as a private member of the class that requires it, in which case it seems autocxx will appropriately add the correct type parameters, but I have no idea if this will cause other issues with the bindings, but at least you'll compile.

eggonabull avatar May 18 '22 02:05 eggonabull