好的,我做了一些研究,显然在这个主题上有很多关于SO的重复问题,仅举几例:
但是我忍不住再提一下,因为
auto -typed返回值,我实际上是复制函数体,唯一的区别是const函数限定符.const版本和非const版本可能返回彼此完全不相容的类型.在这种情况下,斯科特迈耶斯的const_cast习语,以及"非const功能返回非const"技术都不会起作用.举个例子:
struct A {
std::vector<int> examples;
auto get() { return examples.begin(); }
auto get() const { return examples.begin(); }
}; // Do I really have to duplicate?
// My real-world code is much longer
// and there are a lot of such get()'s
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,auto get()返回一个iteratorwhile auto get() const返回一个const_iterator,并且两个不能相互转换(我知道erase(it,it)技巧在这种情况下进行转换,但这不是重点).将const_cast因此无法正常工作; 即使它有效,也需要程序员手动推断auto类型,完全违背使用目的auto.
除了宏之外,真的没办法吗?
好吧,经过一番修改后,我想出了以下两个解决方案,它们允许您保留自动返回类型并且只实现一次 getter。它使用的演员阵容与 Meyer's 的演员阵容相反。
此版本仅返回已实现函数中的两个版本,无论cbegin()您的类型是否有该版本,这都应该作为以下内容的替代cbegin():return static_cast<const A&>(*this).examples.begin();基本上转换为常量并使用普通begin()函数来获取常量。
// Return both, and grab the required one
struct A
{
private:
// This function does the actual getter work, hiding the template details
// from the public interface, and allowing the use of auto as a return type
auto get_do_work()
{
// Your getter logic etc.
// ...
// ...
// Return both versions, but have the other functions extract the required one
return std::make_pair(examples.begin(), examples.cbegin());
}
public:
std::vector<int> examples{ 0, 1, 2, 3, 4, 5 };
// You'll get a regular iterator from the .first
auto get()
{
return get_do_work().first;
}
// This will get a const iterator
auto get() const
{
// Force using the non-const to get a const version here
// Basically the inverse of Meyer's casting. Then just get
// the const version of the type and return it
return const_cast<A&>(*this).get_do_work().second;
}
};
Run Code Online (Sandbox Code Playgroud)
这个应该更好,因为它只返回一个值,并且在编译时就知道获得了哪个值,因此auto知道该怎么做。除此之外,get()功能的工作原理基本相同。
// With if constexpr
struct A
{
private:
// This function does the actual getter work, hiding the template details
// from the public interface, and allowing the use of auto as a return type
template<bool asConst>
auto get_do_work()
{
// Your getter logic etc.
// ...
// ...
if constexpr (asConst)
{
return examples.cbegin();
// Alternatively
// return static_cast<const A&>(*this).examples.begin();
}
else
{
return examples.begin();
}
}
public:
std::vector<int> examples{ 0, 1, 2, 3, 4, 5 };
// Nothing special here, you'll get a regular iterator
auto get()
{
return get_do_work<false>();
}
// This will get a const iterator
auto get() const
{
// Force using the non-const to get a const version here
// Basically the inverse of Meyer's casting, except you get a
// const_iterator as a result, so no logical difference to users
return const_cast<A&>(*this).get_do_work<true>();
}
};
Run Code Online (Sandbox Code Playgroud)
这可能适用于您的自定义类型,也可能不适用于您的自定义类型,但它对我有用,并且它解决了代码重复的需求,尽管它使用了辅助函数。但反过来,实际的吸气剂又变成了俏皮话,所以这应该是合理的。
以下主要函数用于测试这两种解决方案,并按预期工作:
int main()
{
const A a;
*a.get() += 1; // This is an error since it returns const_iterator
A b;
*b.get() += 1; // This works fine
std::cout << *a.get() << "\n";
std::cout << *b.get() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
851 次 |
| 最近记录: |