无法推断模板参数
couldn't deduce template parameter
我正在尝试使用类似于以下内容的 API:
#include<iostream>
#include<boost/optional.hpp>
class Base
{
int id;
public:
int get_id()
{
return id;
}
};
class A : public Base
{
};
class B : public Base
{
};
class M
{
public:
enum Type
{
t_A,
t_B
};
Type type;
boost::optional<A&> a;
boost::optional<B&> b;
boost::optional<A&> get_A()
{
return a;
}
boost::optional<B&> get_B()
{
return b;
}
};
我需要通过任何衍生 class 到达基地。所以我创建了一个这样的模板函数:
template<class T>
boost::optional<T&> get(M & m)
{
switch(m.type)
{
case M::t_A :
return m.get_A();
case M::t_B :
return m.get_B();
default:
throw;
};
}
int main()
{
M m;
//... initialization of m
int i = get<>(m)->get_id();
return 0;
}
但是我的函数的模板参数无法推导:
template_sp_1.cpp:63:17: error: no matching function for call to ‘get(M&)’
int i = get<>(m)->get_id();
^
template_sp_1.cpp:63:17: note: candidate is:
template_sp_1.cpp:46:21: note: template<class T> boost::optional<T&> get(M&)
boost::optional<T&> get(M & m)
^
template_sp_1.cpp:46:21: note: template argument deduction/substitution failed:
template_sp_1.cpp:63:17: note: couldn't deduce template parameter ‘T’
int i = get<>(m)->get_id();
尝试以下任何一种方法都是不可能的;显然是由于使用 boost::optional
:
int i = get<Base>(m)->get_id();
int i = get<A>(m)->get_id();
int i = get<B>(m)->get_id();
对于这样的场景,你们有解决方案吗? (我摸不到API)
编译器错误非常明显:由于 T 不依赖于任何函数参数,并且您没有显式传递该 T,因此编译器无法推断出 T 的值。
请注意,那些 a 和 b 可选类型有不同的类型,因此您的 get() 函数正在尝试 return 多种不同类型(因此您尝试使用模板化可选?)
C++ 不能以这种方式工作,因为类型应该在编译时确定,而您的决定取决于运行时值(开关事物)。考虑 return 变体类型,例如 boost::variant。
正如@Manu343726 已经指出的那样,您的 get()
函数具有不同的 return 类型。但是既然 A
和 B
有共同的基础 class Base
,为什么不使用 Base&
的 return 类型呢?
在 boost 1.58 中修复了关于引用的 bug in boost::optional
,所以您至少需要这个版本。
我修改了你的例子来展示它是如何工作的:
#include <boost/optional.hpp>
#include <boost/version.hpp>
#include <iostream>
#if BOOST_VERSION < 105800
#error boost version must be at least 1.58
#endif
class Base
{
int id;
public:
Base(int id) : id(id) {}
int get_id()
{
return id;
}
};
class A : public Base
{
public:
A() : Base(100) {}
};
class B : public Base
{
public:
B() : Base(999) {}
};
class M
{
public:
enum Type
{
t_A,
t_B
};
Type type;
boost::optional<A&> a;
boost::optional<B&> b;
boost::optional<A&> get_A()
{
return a;
}
boost::optional<B&> get_B()
{
return b;
}
};
Base& get(M & m)
{
switch(m.type)
{
case M::t_A :
return (*(m.get_A()));
case M::t_B :
return (*(m.get_B()));
default:
throw;
};
}
int main()
{
A a;
M m;
m.type = M::t_A;
m.a = a;
Base& base = get(m);
std::cout << base.get_id() << std::endl;
return 0;
}
这个例子将输出:
100
如果 API 确实保证 get_A()
或 get_B()
return 一个包含有效引用的可选值,那么这当然只在运行时有效。
如果这不能保证,你可以使用这样的东西:
boost::optional<Base&> get(M & m)
{
boost::optional<Base&> base;
switch(m.type)
{
case M::t_A:
base = m.get_A();
break;
case M::t_B :
base = m.get_B();
break;
default:
throw;
};
return base;
}
我正在尝试使用类似于以下内容的 API:
#include<iostream>
#include<boost/optional.hpp>
class Base
{
int id;
public:
int get_id()
{
return id;
}
};
class A : public Base
{
};
class B : public Base
{
};
class M
{
public:
enum Type
{
t_A,
t_B
};
Type type;
boost::optional<A&> a;
boost::optional<B&> b;
boost::optional<A&> get_A()
{
return a;
}
boost::optional<B&> get_B()
{
return b;
}
};
我需要通过任何衍生 class 到达基地。所以我创建了一个这样的模板函数:
template<class T>
boost::optional<T&> get(M & m)
{
switch(m.type)
{
case M::t_A :
return m.get_A();
case M::t_B :
return m.get_B();
default:
throw;
};
}
int main()
{
M m;
//... initialization of m
int i = get<>(m)->get_id();
return 0;
}
但是我的函数的模板参数无法推导:
template_sp_1.cpp:63:17: error: no matching function for call to ‘get(M&)’
int i = get<>(m)->get_id();
^
template_sp_1.cpp:63:17: note: candidate is:
template_sp_1.cpp:46:21: note: template<class T> boost::optional<T&> get(M&)
boost::optional<T&> get(M & m)
^
template_sp_1.cpp:46:21: note: template argument deduction/substitution failed:
template_sp_1.cpp:63:17: note: couldn't deduce template parameter ‘T’
int i = get<>(m)->get_id();
尝试以下任何一种方法都是不可能的;显然是由于使用 boost::optional
:
int i = get<Base>(m)->get_id();
int i = get<A>(m)->get_id();
int i = get<B>(m)->get_id();
对于这样的场景,你们有解决方案吗? (我摸不到API)
编译器错误非常明显:由于 T 不依赖于任何函数参数,并且您没有显式传递该 T,因此编译器无法推断出 T 的值。
请注意,那些 a 和 b 可选类型有不同的类型,因此您的 get() 函数正在尝试 return 多种不同类型(因此您尝试使用模板化可选?)
C++ 不能以这种方式工作,因为类型应该在编译时确定,而您的决定取决于运行时值(开关事物)。考虑 return 变体类型,例如 boost::variant。
正如@Manu343726 已经指出的那样,您的 get()
函数具有不同的 return 类型。但是既然 A
和 B
有共同的基础 class Base
,为什么不使用 Base&
的 return 类型呢?
在 boost 1.58 中修复了关于引用的 bug in boost::optional
,所以您至少需要这个版本。
我修改了你的例子来展示它是如何工作的:
#include <boost/optional.hpp>
#include <boost/version.hpp>
#include <iostream>
#if BOOST_VERSION < 105800
#error boost version must be at least 1.58
#endif
class Base
{
int id;
public:
Base(int id) : id(id) {}
int get_id()
{
return id;
}
};
class A : public Base
{
public:
A() : Base(100) {}
};
class B : public Base
{
public:
B() : Base(999) {}
};
class M
{
public:
enum Type
{
t_A,
t_B
};
Type type;
boost::optional<A&> a;
boost::optional<B&> b;
boost::optional<A&> get_A()
{
return a;
}
boost::optional<B&> get_B()
{
return b;
}
};
Base& get(M & m)
{
switch(m.type)
{
case M::t_A :
return (*(m.get_A()));
case M::t_B :
return (*(m.get_B()));
default:
throw;
};
}
int main()
{
A a;
M m;
m.type = M::t_A;
m.a = a;
Base& base = get(m);
std::cout << base.get_id() << std::endl;
return 0;
}
这个例子将输出:
100
如果 API 确实保证 get_A()
或 get_B()
return 一个包含有效引用的可选值,那么这当然只在运行时有效。
如果这不能保证,你可以使用这样的东西:
boost::optional<Base&> get(M & m)
{
boost::optional<Base&> base;
switch(m.type)
{
case M::t_A:
base = m.get_A();
break;
case M::t_B :
base = m.get_B();
break;
default:
throw;
};
return base;
}