[英]Is it possible, in C++, to do some funky stuff when calling my function to return different types depending on my program's needs?
[英]How do I make my function return different results depending on type it's been called using templates in C++
我一直在寻找我的问题的答案很长一段时间,但找不到任何有效的方法。
基本上,我有一个二叉搜索树和一个搜索 function:
T BinarySearchTree::Search(Node *tree, int key) const {
if (tree == nullptr) // if root is null, key is not in tree
return false;
if (key == tree->key) // key is found
return true;
else if (key < tree->key) // recursively look at left subtree if key < tree->key
return Search<T>(tree->left, key);
else // recursively look at right subtree if key > tree->key
return Search<T>(tree->right, key);
}
我想根据调用它的类型返回不同的东西。 例如,如果我将 function 称为Search<bool>()
,我希望它返回真或假,但如果我调用Search<Node*>
,我希望它返回一个指针。 它应该看起来像这样:
T BinarySearchTree::Search(Node *tree, int key) const {
if (key == tree->key){ // key is found
if(T = bool)
return true;
else if(T = Node*)
return tree;
else if (T = int)
return tree->key;
}
我什至不确定这里的模板是否是通往 go 的正确方式,但是任何有关实施的提示都将不胜感激。
如评论中所述,从 API 可用性的角度来看,基于返回类型的重载通常不是一个好主意(并且由于 C++ 不支持开箱即用,因此无论如何都需要一些技巧)。
相反,使用两个不同的 function 名称,这些名称明确说明了它们的作用:
Node* BinarySearchTree::Search(Node* tree, int key) const {
// implement tree search
}
Node const* BinarySearchTree::Search(Node const* tree, int key) const {
return const_cast<Node const*>(Search(const_cast<Node*>(tree), key));
}
bool BinarySearchTree::Contains(Node const* tree, int key) const {
return Search(tree, key) != nullptr;
}
此外,这些函数的公共 API 可能不应该有tree
参数,因为它只在内部需要。
您所要求的可以在 C++17 及更高版本中使用if constexpr
来完成,例如:
#include <type_traits>
template<typename T>
T BinarySearchTree::Search(Node *tree, int key) const {
static_assert(
std::is_same_v<T, bool> ||
std::is_same_v<T, Node*> ||
std::is_same_v<T, int>,
"Invalid type specified");
if (tree == nullptr) {
if constexpr (std::is_same_v<T, bool>)
return false;
}
else if constexpr (std::is_same_v<T, Node*>) {
return nullptr;
}
else {
return 0; // or throw an exception
}
}
else if (key == tree->key) {
if constexpr (std::is_same_v<T, bool>)
return true;
}
else if constexpr (std::is_same_v<T, Node*>) {
return tree;
}
else {
return tree->key;
}
}
else if (key < tree->key)
return Search<T>(tree->left, key);
else
return Search<T>(tree->right, key);
}
在 C++17 之前,使用 SFINAE 可以完成类似的结果,例如:
#include <type_traits>
Node* BinarySearchTree::InternalSearch(Node *tree, int key) const {
if (tree == nullptr) {
return nullptr;
}
else if (key == tree->key) {
return tree;
}
else if (key < tree->key)
return Search<T>(tree->left, key);
else
return Search<T>(tree->right, key);
}
template<typename T>
typename std::enable_if<std::is_same<T, bool>::value, T>::type
BinarySearchTree::Search(Node *tree, int key) const {
return InternalSearch(tree, key) != nullptr;
}
template<typename T>
typename std::enable_if<std::is_same<T, Node*>::value, T>::type
BinarySearchTree::Search(Node *tree, int key) const {
return InternalSearch(tree, key);
}
template<typename T>
typename std::enable_if<std::is_same<T, int>::value, T>::type
BinarySearchTree::Search(Node *tree, int key) const {
tree = InternalSearch(tree, key);
return tree != nullptr ? tree->key : 0 /* or throw an exception */;
}
或者,使用模板专业化,例如:
Node* BinarySearchTree::InternalSearch(Node *tree, int key) const {
if (tree == nullptr) {
return nullptr;
}
else if (key == tree->key) {
return tree;
}
else if (key < tree->key)
return Search<T>(tree->left, key);
else
return Search<T>(tree->right, key);
}
template<typename T>
T BinarySearchTree::Search(Node *tree, int key) const {
// throw a suitable exception
}
template<>
bool BinarySearchTree::Search<bool>(Node *tree, int key) const {
return InternalSearch(tree, key) != nullptr;
}
template<>
Node* BinarySearchTree::Search<Node*>(Node *tree, int key) const {
return InternalSearch(tree, key);
}
template<>
int BinarySearchTree::Search<int>(Node *tree, int key) const {
tree = InternalSearch(tree, key);
return tree != nullptr ? tree->key : 0 /* or throw an exception */;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.