У меня есть два класса test1 и test2:
struct test1
{
int r;
test1() {}
test1(int value) : r(value) {}
test1 foo() const;
};
struct test2
{
int r;
test2() {}
test2(int value) : r(value) {}
};
template <typename t>
struct data_t
{
static const t one;
};
template <> const test1 data_t<test1>::one = test1(1);
template <> const test2 data_t<test2>::one = test2(1);
Затем я создал функцию для чего-то:
template <typename t, const t& value>
t do_somthing()
{ return value; };
действие do_something простое, оно возвращает копию значения, поэтому в основной функции:
int main()
{
test1 r = do_somthing<test1, data_t<test1>::one>();
}
проблема возникает при реализации test1 :: foo
test1 test1::foo() const
{ return do_somthing<test1, *this>(); }
компилятор останавливается с ошибкой:
'this' : can only be referenced inside non-static member functions
с *this
становится test1 const&
, что приемлемо в качестве второго параметра, так почему эта ошибка?