C++ 多重继承类中非类型模板参数重载成员函数的消歧
我有以下小程序:C++ 多重继承类中非类型模板参数重载成员函数的消歧,c++,templates,c++17,C++,Templates,C++17,我有以下小程序: #include <iostream> class Field1 { using Self_t = Field1; public: template<unsigned int KEY, std::enable_if_t<KEY == 1, Self_t>*> int get() { return 1; } }; class Field2 { using Self_t = Field
#include <iostream>
class Field1 {
using Self_t = Field1;
public:
template<unsigned int KEY, std::enable_if_t<KEY == 1, Self_t>*>
int get() {
return 1;
}
};
class Field2 {
using Self_t = Field2;
public:
template<unsigned int KEY, std::enable_if_t<KEY == 2, Self_t>*>
char get() {
return 'a';
}
};
template<typename FIRST, typename ... REST>
class _Container<FIRST, REST...> : public FIRST, public REST ... {
using FIRST::get;
using REST::get ...;
};
template<typename ... FIELDS>
class Container: public _Container<FIELDS...> {};
int main(int, char*[]) {
Container<Field1, Field2> fred;
std::cout << fred.get<1>() << std::endl;
return 0;
}
#包括
类字段1{
使用Self_t=Field1;
公众:
模板
int get(){
返回1;
}
};
类字段2{
使用Self_t=Field2;
公众:
模板
char get(){
返回“a”;
}
};
模板
类容器:公共优先,公共休息。。。{
使用FIRST::get;
使用REST::get。。。;
};
模板
类容器:公共_容器{};
int main(int,char*[]){
集装箱;
std::cout模板
int get(){
返回1;
}
// ...
f、 get();
此函数接受两个模板参数,但您只提供了一个。或者在调用函数时也传递第二个模板参数,或者为第二个模板参数提供一个默认参数,以便只需要一个。我也不认为这里需要Self\t
模板
int get(){
返回1;
}
模板
int get(){
返回1;
}
// ...
f、 get();
此函数接受两个模板参数,但您只提供了一个。或者在调用函数时也传递第二个模板参数,或者为第二个模板参数提供一个默认参数,以便只需要一个。我也不认为这里需要Self\t
模板
int get(){
返回1;
}
非常感谢。整个周末我都在为这个忙。非常感谢。整个周末我都在为这个忙。
./src/main.cpp: In function ‘int main(int, char**)’:
../src/main.cpp:34:27: error: no matching function for call to ‘Container<Field1, Field2>::get<1>()’
34 | std::cout << fred.get<1>() << std::endl;
| ^
../src/main.cpp:8:6: note: candidate: ‘template<unsigned int KEY, std::enable_if_t<(KEY == 1), Field1>* <anonymous> > int Field1::get()’
8 | int get() {
| ^~~
../src/main.cpp:8:6: note: template argument deduction/substitution failed:
../src/main.cpp:34:27: note: couldn’t deduce template parameter ‘<anonymous>’
34 | std::cout << fred.get<1>() << std::endl;
| ^
../src/main.cpp:17:7: note: candidate: ‘template<unsigned int KEY, std::enable_if_t<(KEY == 2), Field2>* <anonymous> > char Field2::get()’
17 | char get() {
| ^~~
../src/main.cpp:17:7: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/10.2.0/bits/move.h:57,
from /usr/include/c++/10.2.0/bits/nested_exception.h:40,
from /usr/include/c++/10.2.0/exception:148,
from /usr/include/c++/10.2.0/ios:39,
from /usr/include/c++/10.2.0/ostream:38,
from /usr/include/c++/10.2.0/iostream:39,
from ../src/main.cpp:1:
/usr/include/c++/10.2.0/type_traits: In substitution of ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = Field2]’:
../src/main.cpp:16:69: required from here
/usr/include/c++/10.2.0/type_traits:2554:11: error: no type named ‘type’ in ‘struct std::enable_if<false, Field2>’
2554 | using enable_if_t = typename enable_if<_Cond, _Tp>::type;
| ^~~~~~~~~~~
../src/main.cpp:36:20: error: no matching member function for call to 'get'
std::cout << fred.get<1>() << std::endl;
~~~~~^~~~~~
../src/main.cpp:9:6: note: candidate template ignored: couldn't infer template argument ''
int get() {
^
../src/main.cpp:19:7: note: candidate template ignored: couldn't infer template argument ''
char get() {
^