Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIG 3使用模板构造函数包装未模板类 我们有一个模板化的构造函数的非模板C++类。我们能够使用SWIG 2来制作Python包装器,但同样的代码在SWIG 3中失败:包装器类的构造函数引发AttributeErrorNo构造函数定义。我希望有人能提出一个干净的解决方案 这里C++标题的摘录: class FootprintSet { public: template <typename ImagePixelT> FootprintSet(image::Image<ImagePixelT> const& img, Threshold const& threshold, int const npixMin=1, bool const setPeaks=true); FootprintSet(geom::Box2I region); ..._Python_C++_Templates_Constructor_Swig - Fatal编程技术网

SWIG 3使用模板构造函数包装未模板类 我们有一个模板化的构造函数的非模板C++类。我们能够使用SWIG 2来制作Python包装器,但同样的代码在SWIG 3中失败:包装器类的构造函数引发AttributeErrorNo构造函数定义。我希望有人能提出一个干净的解决方案 这里C++标题的摘录: class FootprintSet { public: template <typename ImagePixelT> FootprintSet(image::Image<ImagePixelT> const& img, Threshold const& threshold, int const npixMin=1, bool const setPeaks=true); FootprintSet(geom::Box2I region); ...

SWIG 3使用模板构造函数包装未模板类 我们有一个模板化的构造函数的非模板C++类。我们能够使用SWIG 2来制作Python包装器,但同样的代码在SWIG 3中失败:包装器类的构造函数引发AttributeErrorNo构造函数定义。我希望有人能提出一个干净的解决方案 这里C++标题的摘录: class FootprintSet { public: template <typename ImagePixelT> FootprintSet(image::Image<ImagePixelT> const& img, Threshold const& threshold, int const npixMin=1, bool const setPeaks=true); FootprintSet(geom::Box2I region); ...,python,c++,templates,constructor,swig,Python,C++,Templates,Constructor,Swig,以及SWIG接口文件的主要部分: %shared_ptr(lsst::afw::detection::FootprintSet); %include "lsst/afw/detection/FootprintSet.h" %define %footprintSetOperations(PIXEL) %template(FootprintSet) FootprintSet<PIXEL>; %enddef %extend lsst::afw::detection::Footprin

以及SWIG接口文件的主要部分:

%shared_ptr(lsst::afw::detection::FootprintSet);

%include "lsst/afw/detection/FootprintSet.h"

%define %footprintSetOperations(PIXEL)
%template(FootprintSet) FootprintSet<PIXEL>;
%enddef

%extend lsst::afw::detection::FootprintSet {
%footprintSetOperations(boost::uint16_t)
%footprintSetOperations(int)
%footprintSetOperations(float)
%footprintSetOperations(double)
}
我考虑过的一个粗略的解决方法是用每个专门化的显式版本替换头部中的模板构造函数,例如:

class FootprintSet {
public:
#ifndef SWIG
    template <typename ImagePixelT>
    FootprintSet(image::Image<ImagePixelT> const& img,
                 Threshold const& threshold,
                 int const npixMin=1, bool const setPeaks=true);
#else
    FootprintSet(image::Image<boost::unit16> const& img,
                 Threshold const& threshold,
                 int const npixMin=1, bool const setPeaks=true);
    FootprintSet(image::Image<int> const& img,
                 Threshold const& threshold,
                 int const npixMin=1, bool const setPeaks=true);
    FootprintSet(image::Image<float> const& img,
                 Threshold const& threshold,
                 int const npixMin=1, bool const setPeaks=true);
    FootprintSet(image::Image<double> const& img,
                 Threshold const& threshold,
                 int const npixMin=1, bool const setPeaks=true);
#endif

    FootprintSet(geom::Box2I region);
...

或最好在SWIG接口中添加类似的东西,而不是C++头。< /P>


不过,我还是希望有一个更简单的解决方案。我们想更新到SWIG 3以获得对C++11的支持,但这是一个重要的拦截器。

编译器可能需要一些帮助来确定映像是模板类型。尝试输入typename:


这似乎是SWIG的一个倒退。下面是一个简化的示例:

price@price-laptop:~/test $ cat test.h
#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <typeinfo>

namespace test {

class Foo
{
public:
    template<typename T>
    Foo(T bar);
    ~Foo() {}
    void working() const {
        std::cout << "WORKING" << std::endl;
    }
};

}

#endif
price@price-laptop:~/test $ cat test.cc 
#include "test.h"

namespace test {

template <typename T>
Foo::Foo(T) {
    std::cout << typeid(T).name() << std::endl;
}

template Foo::Foo(int);

}

price@price-laptop:~/test $ cat test.i
%feature("autodoc", "1");
%module(package="test", docstring="test") testLib

%{
#include "test.h"
%}

%include "test.h"

%extend test::Foo {
     %template(Foo) Foo<int>;
}

price@price-laptop:~/test $ clang++ -shared -undefined dynamic_lookup -o libtest.dylib test.cc
在SWIG 3.0.2中,它似乎将Foo::Foo视为一个普通函数,警告它没有返回类型,并忽略它:

price@price-laptop:~/test $ swig -python -c++ test.i
test.i:11: Warning 504: Function test::Foo::Foo(int) must have a return type. Ignored.
price@price-laptop:~/test $ clang++ -shared -undefined dynamic_lookup -o _testLib.so test_wrap.cxx -L. -ltest -I/usr/include/python2.7
price@price-laptop:~/test $ python -c "import testLib; testLib.Foo(1)"Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "testLib.py", line 82, in __init__
    def __init__(self, *args, **kwargs): raise AttributeError("No constructor defined")
AttributeError: No constructor defined
建议你把它踢向上游


编辑:这是一个有趣的建议。我试过了,但不幸的是没用。谢谢。我将其报告为SWIG问题;使用您的独立示例。
price@price-laptop:~/test $ swig -python -c++ test.i
price@price-laptop:~/test $ clang++ -shared -undefined dynamic_lookup -o _testLib.so test_wrap.cxx -L. -ltest -I/usr/include/python2.7
price@price-laptop:~/test $ python -c "import testLib; testLib.Foo(1)"
i
price@price-laptop:~/test $ swig -python -c++ test.i
test.i:11: Warning 504: Function test::Foo::Foo(int) must have a return type. Ignored.
price@price-laptop:~/test $ clang++ -shared -undefined dynamic_lookup -o _testLib.so test_wrap.cxx -L. -ltest -I/usr/include/python2.7
price@price-laptop:~/test $ python -c "import testLib; testLib.Foo(1)"Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "testLib.py", line 82, in __init__
    def __init__(self, *args, **kwargs): raise AttributeError("No constructor defined")
AttributeError: No constructor defined