Menu

#1317 template constructor of template class not recognized

None
closed
None
5
2023-07-30
2013-04-19
Job
No

I get this warning while compiling the class below (with %template(MyClassImpl) MyClass<int, int="">;).
Warning 317: Specialization of non-template 'MyClass'.

I think SWIG is misinterpreting the template definition of the constructor as a specialization for the class template.

template <typename T, typename U>
struct MyClass
{
    template<typename P>
    MyClass<T,D>(P p)
    {
    }
};

Discussion

  • Olly Betts

    Olly Betts - 2022-01-23

    Still reproducible with current git master.

    As a workaround, you can omit the <T,D> on the constructor in this situation which SWIG does accept :

    template <typename T, typename U>
    struct MyClass
    {
        template<typename P>
        MyClass(P p)
        {
        }
    };
    
     
  • Olly Betts

    Olly Betts - 2022-01-23

    SWIG also parses the original example if the template<typename P> is removed - that obviously changes the meaning but shows that the redundant template parameters on the constructor aren't the sole cause of this. So the problem here seems to be handling the combination of a constructor in a templated class itself being a template and having explicit ly listed template parameters from the class.

     

    Last edit: Olly Betts 2022-01-23
  • Olly Betts

    Olly Betts - 2022-09-22

    Still reproducible with current git master.

    The original testcase needs a tweak to be valid C++ I think - presumably D should be U otherwise GCC complains about it:

    test.i:8:15: error: ‘D’ was not declared in this scope
        8 |     MyClass<T,D>(P p)
          |               ^
    test.i:8:16: error: template argument 2 is invalid
        8 |     MyClass<T,D>(P p)
          |                ^
    

    But SWIG behaves the same with that fixed.

     
  • William Fulton

    William Fulton - 2023-07-30
    • status: open --> closed
    • assigned_to: William Fulton
    • Group: -->
     
  • William Fulton

    William Fulton - 2023-07-30

    Corrected testcase:

    %inline %{
    template <typename T, typename U>
    struct MyClass
    {
        template<typename P>
    //    MyClass<T,D>(P p)
        MyClass<T,U>(P p)
        {
        }
    };
    %}
    %template(MyClassImpl) MyClass<int, int>;
    

    Note that MyClass<T,U> is not valid syntax from C++20 onwards, but SWIG continues to support it.
    The warning no longer appears in master, so this issue is fixed for swig-4.2.0.

     
  • Olly Betts

    Olly Betts - 2023-07-30

    Great - 2ff9da0ce625eabf0dde3ffeaec075eba2c733a8 was the exact commit fixing this for the record.

     

Log in to post a comment.