模板化运算符过载无法解析

时间:2015-01-23 05:36:35

标签: c++ templates operator-overloading

我试图在结构中包含一个值,但是我无法让编译器识别我的operator=函数。

namespace details
{
    template<typename Value, Value Default>
    struct value_wrapper_
    {
        typedef Value value_type;
        typedef value_wrapper_<Value, Default> type;

        value_type value = Default;

        explicit value_wrapper_() = default;
        explicit value_wrapper_(const value_type& value) :
            value(value)
        {
        }

        operator value_type() const
        {
            return value;
        }

        type& operator=(value_type value)
        {
            this.value = value;

            return *this;
        }
    };
}

struct gpu_id_t : details::value_wrapper_<unsigned int, 0> { };

int main()
{
    gpu_id_t id;

    id = 42;
}

我得到的错误是:

prog.cpp: In function 'int main()':
prog.cpp:38:6: error: no match for 'operator=' (operand types are 'gpu_id_t' and 'int')
   id = 42;
      ^
prog.cpp:38:6: note: candidates are:
prog.cpp:32:9: note: gpu_id_t& gpu_id_t::operator=(const gpu_id_t&)
  struct gpu_id_t : details::value_wrapper_<unsigned int, 0> { };
         ^
prog.cpp:32:9: note:   no known conversion for argument 1 from 'int' to 'const gpu_id_t&'
prog.cpp:32:9: note: gpu_id_t& gpu_id_t::operator=(gpu_id_t&&)
prog.cpp:32:9: note:   no known conversion for argument 1 from 'int' to 'gpu_id_t&&'

我觉得我错过了一些明显的东西,感谢任何帮助。

提前致谢!

0 个答案:

没有答案