Andrew Downing 0 Posted October 15, 2022 class A<Class T> { void A() { PrintFormat("this is %1", Type()); // this is A<@B> PrintFormat("T is %1", T); // T is B } void bar(A<T> other) {} } class B {} void foo() { auto a = new A<ref B>(); a.bar(new A<ref B>()); //Error: Types 'A<@B>' and 'A<B>' are unrelated auto arr1 = new array<ref B>(); auto arr2 = new array<ref B>(); arr1.InsertArray(arr2); //Error: Types 'array<@B>' and 'array<B>' are unrelated } array.InsertArray(array<T> other) or any template function that uses T without the `ref` qualifier when the class was instantiated with a strong reference type parameter seems to be broken. The source of the problem seems to be that the `ref` qualifier is dropped from the template type parameter. If you add `ref` before T in `bar`, there is no error. If this wasn't just a mistake I'd guess that it was done because you can always add the `ref` qualifier on the template parameter, but not take it away. I think this causes more harm than good though because now you can't actually write a generic function. You need to write two versions of the function, one with `ref` and one without. To me the reasonable thing to do would be to retain the `ref` qualifier on the type parameter and provide a way to remove it if needed. Something like void bar(A<remove_ref<T>> other). Share this post Link to post Share on other sites
Sid Debian 132 Posted October 17, 2022 On 10/15/2022 at 9:33 PM, Andrew Downing said: class A<Class T> { void A() { PrintFormat("this is %1", Type()); // this is A<@B> PrintFormat("T is %1", T); // T is B } void bar(A<T> other) {} } class B {} void foo() { auto a = new A<ref B>(); a.bar(new A<ref B>()); //Error: Types 'A<@B>' and 'A<B>' are unrelated auto arr1 = new array<ref B>(); auto arr2 = new array<ref B>(); arr1.InsertArray(arr2); //Error: Types 'array<@B>' and 'array<B>' are unrelated } array.InsertArray(array<T> other) or any template function that uses T without the `ref` qualifier when the class was instantiated with a strong reference type parameter seems to be broken. The source of the problem seems to be that the `ref` qualifier is dropped from the template type parameter. If you add `ref` before T in `bar`, there is no error. If this wasn't just a mistake I'd guess that it was done because you can always add the `ref` qualifier on the template parameter, but not take it away. I think this causes more harm than good though because now you can't actually write a generic function. You need to write two versions of the function, one with `ref` and one without. To me the reasonable thing to do would be to retain the `ref` qualifier on the type parameter and provide a way to remove it if needed. Something like void bar(A<remove_ref<T>> other). Actually it's thqe way of C++ problem (game engine written on it. So yeah use stong declination in lists or you'll get future troubles, coz MS VC has own cleaning mechanics that may remove your data from list because the cleaning system considered 'em as not needed (not using). Share this post Link to post Share on other sites