如何获取模板class的值?
How to get the value of a template class?
我正在尝试获取模板的值 class。要获得 class 的值,我可以很容易地这样做:
int get_value()
{
return *this;
}
但我想创建一个 class,并扩展它,并且不要再在所有 class 中创建 get_value()
。所以,我这样做了:
template<typename T>
class _extend : public T
{
public:
auto _get_value()
{
return *this;
}
};
template<typename T>
class extend : public _extend<T>
{
public:
T get_value()
{
auto value = this->_get_value(); /* It was `_get_value()`, changed to `this->_get_value()` due to the comments */
T output = value;
return output;
}
};
但它不起作用:没有输出。
编辑
示例程序:
#include <iostream>
namespace kc
{
template<typename T>
class _extend : public T
{
public:
auto _get_value()
{
return *this;
}
};
template<typename T>
class extend : public _extend<T>
{
public:
T get_value()
{
auto value = this->_get_value();
T output = value;
return output;
}
};
class A : public std::string, public extend<std::string>
{
public:
using std::string::string;
};
}
int main()
{
kc::A a("a");
std::cout << a.get_value() << std::endl;
}
题中代码的问题是多重继承。 _extend<string>::get_value()
将被调用的 *this
不是具有值的字符串。
虽然我同意@NicolBolas 在评论中的观点,但我不明白你为什么想要这个,但你可以制作一个 class 模板,它将 return 其派生的价值只有一级继承。您只需要给它一个完美的转发构造函数并将 get_value()
强制转换为基本类型,即
#include <iostream>
#include <string>
namespace kc
{
template<typename T>
class extend : public T
{
public:
template<typename... Args>
extend(Args&&... args) : T(std::forward<Args>(args)...)
{}
T get_value()
{
return *static_cast<T*>(this);
}
};
}
int main()
{
kc::extend<std::string> a("foo");
std::cout << a.get_value() << std::endl;
}
基本上:
template<class C>
class extend : public C
{
public:
using C::C;
auto get()
{
return *this;
}
};
还有一个完整的例子:
#include <iostream>
template<class C>
class extend : public C
{
public:
using C::C;
auto get()
{
return *this;
}
};
class e_string : public extend<std::string>
{
public:
using extend<std::string>::extend;
};
int main()
{
e_string s = "Test";
std::cout << s.get() << std::endl;
}
我正在尝试获取模板的值 class。要获得 class 的值,我可以很容易地这样做:
int get_value()
{
return *this;
}
但我想创建一个 class,并扩展它,并且不要再在所有 class 中创建 get_value()
。所以,我这样做了:
template<typename T>
class _extend : public T
{
public:
auto _get_value()
{
return *this;
}
};
template<typename T>
class extend : public _extend<T>
{
public:
T get_value()
{
auto value = this->_get_value(); /* It was `_get_value()`, changed to `this->_get_value()` due to the comments */
T output = value;
return output;
}
};
但它不起作用:没有输出。
编辑
示例程序:
#include <iostream>
namespace kc
{
template<typename T>
class _extend : public T
{
public:
auto _get_value()
{
return *this;
}
};
template<typename T>
class extend : public _extend<T>
{
public:
T get_value()
{
auto value = this->_get_value();
T output = value;
return output;
}
};
class A : public std::string, public extend<std::string>
{
public:
using std::string::string;
};
}
int main()
{
kc::A a("a");
std::cout << a.get_value() << std::endl;
}
题中代码的问题是多重继承。 _extend<string>::get_value()
将被调用的 *this
不是具有值的字符串。
虽然我同意@NicolBolas 在评论中的观点,但我不明白你为什么想要这个,但你可以制作一个 class 模板,它将 return 其派生的价值只有一级继承。您只需要给它一个完美的转发构造函数并将 get_value()
强制转换为基本类型,即
#include <iostream>
#include <string>
namespace kc
{
template<typename T>
class extend : public T
{
public:
template<typename... Args>
extend(Args&&... args) : T(std::forward<Args>(args)...)
{}
T get_value()
{
return *static_cast<T*>(this);
}
};
}
int main()
{
kc::extend<std::string> a("foo");
std::cout << a.get_value() << std::endl;
}
基本上:
template<class C>
class extend : public C
{
public:
using C::C;
auto get()
{
return *this;
}
};
还有一个完整的例子:
#include <iostream>
template<class C>
class extend : public C
{
public:
using C::C;
auto get()
{
return *this;
}
};
class e_string : public extend<std::string>
{
public:
using extend<std::string>::extend;
};
int main()
{
e_string s = "Test";
std::cout << s.get() << std::endl;
}