c++templatesc++20

Elegantly tell content-assist that my "S_Something" class is really always "S<....something....>"


Problem

I have S<?,?,?,?> as a template class.
By design, a function f() always accepts a parameter of type S<?,?,?,?>*.

The below code works fine, but I lose the content-assist quality-of-life.

template<
     class Src_Param
    ,class Dst_Param
    ,int X1_Param
    ,int X2_Param
>class S{public:
    using Src=Src_Param;
    using Dst=Dst_Param;
    static constexpr int X1=X1_Param;
    static constexpr int X2=X2_Param;
};
template<class S_Something> static void f(S_Something* s){
    s->                  ///<== test 1
}

When press -> or ctrl+enter, here is the assist I get from IDE.
It is sad and empty. ::-

enter image description here

My poor workaround

I create a cosmetic alias named Self.

The new alias is simple.
In every use-case, Self<T> = T, and T here is always S<....something....> (by design).

template<class Param> using Self
     =S<typename Param::Src,typename Param::Dst,Param::X1,Param::X2>;  
template<class S_Something> static void f(S_Something* s){
    using S2=Self<S_Something>;
    S2* s2=s;
    s2->                 ///<== test 2
    S2::                 ///<== test 3
}

Now, I got my sweet content-assist (intellisense) back. It works.

Here is the result from test2.

enter image description here

Here is the result from test3.

enter image description here

^^ It works and it is beautiful.

Question

Unfortunately, my new Self alias decreases the maintainability.

If I need to add more template parameters like S::X3_Param, the Self have to be updated as well.

Is there a way to hint content-assist (preferably by using proper syntax), that S_Something inside f() is always S<...something...>, without sacrifice performance?

I am using Visual Studio, but I prefer a solution that does not depend on IDE.


Solution

  • By design, a function f() always accepts a parameter of type S*.

    No, currently you haven't placed any restriction on the template parameter S_Something so at the time of writing the code there is no way to know what comes after s->.

    You can solve this by changing f so that f's function parameter is of type S<S_Something>. Doing this will also give make your other test cases work.

    //the template parameters can be deduced and you'll get the suggestions after s-> etc
    template<class Src, class Dst, int X1, int X2> static void f(S<Src, Dst, X1, X2>*);