Correct way to call user defined conversion operator in templated function with MSVC

Viewed 42

I need to call a templated conversion operator inside a class like this :


struct S {
    template<typename T>
    operator T() const {
        return T{};
    }

    template<typename T>
    T test()
    {
        return operator T();
    }
};

int main(){
    S s;
    s.test<int>();

    return 0;
}

This code compiles with clang but not with MSVC(19), gives me a c2352 error. Is the correct syntax?

Demo

1 Answers

Syntax is correct, as work around you might be explicit with this->operator T():

template<typename T>
T test()
{
    return this->operator T();
}

Demo

Related