Why can't C++ deduce template type from assignment?

后端 未结 5 1075
盖世英雄少女心
盖世英雄少女心 2020-12-06 09:39

int x = fromString(\"test\") :could not deduce template argument for \'ValueType\'

int x = fromString(\"test\") : works

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-06 10:22

    C++ doesn't do type inference on the return value. I.e., the fact that it is being assigned to an int isn't used in template parameter deduction.

    (Removed edit, since someone else presented the overloaded cast solution already.)

提交回复
热议问题