C++ Template problem adding two data types

后端 未结 9 1319
慢半拍i
慢半拍i 2021-02-13 02:51

I have a template class with an overloaded + operator. This is working fine when I am adding two ints or two doubles. How do I get it to add and int and a double and return th

9条回答
  •  梦毁少年i
    2021-02-13 03:30

    This is technically possible by defining an implicit case to TemplateTest:

    operator TemplateTest() {
        return TemplateTest((double)x);
    }
    

    Practically this probably isn't a great idea though, as x can't necessarily be safely cast to a double; it happens that you're using a TemplateTest here, but you could be using a TemplateTest later. You should probably rethink what you're doing and decide if you're sure you actually need this behavior

提交回复
热议问题