具有三元运算符和模板的自动
auto with ternary operator and templates
我有模板class
template <typename T>
class foo;
T有2个有效值,对应于:
using fooT1 = class foo<T1>;
using fooT2 = class foo<T2>;
我想编写如下代码:
const auto* fooPtr = useFooT1 ? getFooT1Ptr() : getFooT2Ptr();
因为在此函数中使用 fooPtr
的代码不依赖于 fooPtr 的类型是 fooT1
还是 fooT2
但是,我收到以下编译器错误:
error: conditional expression between distinct pointer types ...
我明白,根据 C++ 标准,应该有一个共同的类型,两者都可以转换为,所以这种方法可能行不通。
在不复制大量代码的情况下实现此功能的好方法是什么?
的确,C++ 是静态类型的,所以变量的类型不能依赖于运行时间条件。
相反,将通用代码放入模板中:
template <typename T> doStuff(foo<T> * f) {
// stuff that works with any `foo` type
}
并根据 运行 时间变量调用不同的特化
if (useFooT1) {
doStuff(getFooT1Ptr());
} else {
doStuff(getFooT2Ptr());
}
如果布尔变量 useFooT1 在编译时已知,您可以实现编译时开关,例如
FooT1 const* getFooPtr(std::true_type /* useFooT1 */) {
return getFooT1Ptr();
}
FooT2 const* getFooPtr(std::false_type /* !useFooT1 */) {
return getFooT2Ptr();
}
/* ... */
auto const* fooPtr = getFooPtr(std::integral_constant<bool, useFooT1>());
这也可以推广到两种以上的类型,开关可以直接取决于类型 T1/T2。
另请参阅:Is it possible to use tag dispatching to determine return type and Switch passed type from template 和许多其他内容。
下面是一个示例(实际上是 2 种不同的方法:V1/V2)如何使用一个可以在内部使用不同类型的 std:: 容器的函数。请注意,使用的容器类型实际上可以改变函数的结果(因为容器的内部排序),这就是我在这里展示的内容。函数 returnMaxPointV1/2 接受一个 bool 参数,内部有部分通用代码和部分专用代码。这是与使用模板相反的方法,其中函数调用者必须专门化(!在编译时!),但函数体是通用的。这里的调用者是通用的(!在运行时!),但主体是专门的。
#include <vector>
#include <set>
#include <iostream>
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
struct Point2D {
int x1;
int x2;
bool operator < (const Point2D& rhs) const {
return x1-10*x2 < rhs.x1-10*rhs.x2;
}
};
Point2D points[4] = { { 10,2 },{ 2,2 },{ 0,1 },{ 10,4 } };
namespace returnMaxInlines
{
inline Point2D FindMax(Point2D a, Point2D b) {
return (a.x1 > b.x1) ? a : b;
}
}
/*
The function returnMaxPoint fills a container with points that satisfy the condition x2 >= 2.
You can select what kind of containter to use by using the bool containterTypeSet
the function then finds the point with maximum x1 in the container, but because
there are some points with the same x1 it will return the first one encountered,
which may be a different one depending on the container type,
because the points may be differently ordered in the container.
*/
Point2D returnMaxPointV1(bool containterTypeSet)
{
using namespace returnMaxInlines; // the inline function is only accessible locally in this function (because of the namespace)
Point2D maxPoint = { 0,0 };
if (containterTypeSet == true) {
std::set<Point2D> container;
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
container.insert(points[cnt]);
}
}
for (auto it = container.begin(); it != container.end(); ++it) {
maxPoint = FindMax(maxPoint, *it); // this part of the code is generic for both cases of bool containterTypeSet, it is an inline function
}
}
else {
std::vector<Point2D> container;
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
container.push_back(points[cnt]);
}
}
for (auto it = container.begin(); it != container.end(); ++it) {
maxPoint = FindMax(maxPoint, *it); // this part of the code is generic for both cases of bool containterTypeSet, it is an inline function
}
}
return maxPoint;
}
/* Alternative implementation
No inline function is needed, but you need to have all variables for both options declared in scope
and you need to add many if-statements, thus branching (but maybe the compiler can optimize this out (which will yield some kind of version V1)
*/
Point2D returnMaxPointV2(bool containterTypeSet)
{
Point2D maxPoint = { 0,0 };
std::set<Point2D> containerSet;
std::vector<Point2D> containerVector;
// specialized code
if (containterTypeSet) {
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
containerSet.insert(points[cnt]);
}
}
}
else{
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
containerVector.push_back(points[cnt]);
}
}
}
std::set<Point2D>::iterator SetIterator;
std::vector<Point2D>::iterator VectorIterator;
const Point2D *ScopePoint;
bool foolCompiler; // the compiler was tripping if both types of the tenary operator (?:) were not of the same type, so I made the statement such that the statement was of type bool.
for(foolCompiler = containterTypeSet? ((SetIterator = containerSet.begin()) == SetIterator): ((VectorIterator = containerVector.begin()) == VectorIterator);
containterTypeSet ? SetIterator != containerSet.end() : VectorIterator != containerVector.end();
foolCompiler = containterTypeSet ? ((SetIterator++) == SetIterator) : ((VectorIterator++) == VectorIterator)
){
ScopePoint = containterTypeSet ? &(*SetIterator) : &(*VectorIterator);
// generic code
maxPoint = (maxPoint.x1 > ScopePoint->x1) ? maxPoint : *ScopePoint;
}
return maxPoint;
}
int main()
{
Point2D result;
result = returnMaxPointV1(true);
std::cout << "result1: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV1(false);
std::cout << "result2: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV2(true);
std::cout << "result3: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV2(false);
std::cout << "result4: (" << result.x1 << "," << result.x2 << ")" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
}
程序的输出是:
result1: (10,2)
result2: (10,4)
result3: (10,2)
result4: (10,4)
我有模板class
template <typename T>
class foo;
T有2个有效值,对应于:
using fooT1 = class foo<T1>;
using fooT2 = class foo<T2>;
我想编写如下代码:
const auto* fooPtr = useFooT1 ? getFooT1Ptr() : getFooT2Ptr();
因为在此函数中使用 fooPtr
的代码不依赖于 fooPtr 的类型是 fooT1
还是 fooT2
但是,我收到以下编译器错误:
error: conditional expression between distinct pointer types ...
我明白,根据 C++ 标准,应该有一个共同的类型,两者都可以转换为,所以这种方法可能行不通。
在不复制大量代码的情况下实现此功能的好方法是什么?
的确,C++ 是静态类型的,所以变量的类型不能依赖于运行时间条件。
相反,将通用代码放入模板中:
template <typename T> doStuff(foo<T> * f) {
// stuff that works with any `foo` type
}
并根据 运行 时间变量调用不同的特化
if (useFooT1) {
doStuff(getFooT1Ptr());
} else {
doStuff(getFooT2Ptr());
}
如果布尔变量 useFooT1 在编译时已知,您可以实现编译时开关,例如
FooT1 const* getFooPtr(std::true_type /* useFooT1 */) {
return getFooT1Ptr();
}
FooT2 const* getFooPtr(std::false_type /* !useFooT1 */) {
return getFooT2Ptr();
}
/* ... */
auto const* fooPtr = getFooPtr(std::integral_constant<bool, useFooT1>());
这也可以推广到两种以上的类型,开关可以直接取决于类型 T1/T2。
另请参阅:Is it possible to use tag dispatching to determine return type and Switch passed type from template 和许多其他内容。
下面是一个示例(实际上是 2 种不同的方法:V1/V2)如何使用一个可以在内部使用不同类型的 std:: 容器的函数。请注意,使用的容器类型实际上可以改变函数的结果(因为容器的内部排序),这就是我在这里展示的内容。函数 returnMaxPointV1/2 接受一个 bool 参数,内部有部分通用代码和部分专用代码。这是与使用模板相反的方法,其中函数调用者必须专门化(!在编译时!),但函数体是通用的。这里的调用者是通用的(!在运行时!),但主体是专门的。
#include <vector>
#include <set>
#include <iostream>
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
struct Point2D {
int x1;
int x2;
bool operator < (const Point2D& rhs) const {
return x1-10*x2 < rhs.x1-10*rhs.x2;
}
};
Point2D points[4] = { { 10,2 },{ 2,2 },{ 0,1 },{ 10,4 } };
namespace returnMaxInlines
{
inline Point2D FindMax(Point2D a, Point2D b) {
return (a.x1 > b.x1) ? a : b;
}
}
/*
The function returnMaxPoint fills a container with points that satisfy the condition x2 >= 2.
You can select what kind of containter to use by using the bool containterTypeSet
the function then finds the point with maximum x1 in the container, but because
there are some points with the same x1 it will return the first one encountered,
which may be a different one depending on the container type,
because the points may be differently ordered in the container.
*/
Point2D returnMaxPointV1(bool containterTypeSet)
{
using namespace returnMaxInlines; // the inline function is only accessible locally in this function (because of the namespace)
Point2D maxPoint = { 0,0 };
if (containterTypeSet == true) {
std::set<Point2D> container;
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
container.insert(points[cnt]);
}
}
for (auto it = container.begin(); it != container.end(); ++it) {
maxPoint = FindMax(maxPoint, *it); // this part of the code is generic for both cases of bool containterTypeSet, it is an inline function
}
}
else {
std::vector<Point2D> container;
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
container.push_back(points[cnt]);
}
}
for (auto it = container.begin(); it != container.end(); ++it) {
maxPoint = FindMax(maxPoint, *it); // this part of the code is generic for both cases of bool containterTypeSet, it is an inline function
}
}
return maxPoint;
}
/* Alternative implementation
No inline function is needed, but you need to have all variables for both options declared in scope
and you need to add many if-statements, thus branching (but maybe the compiler can optimize this out (which will yield some kind of version V1)
*/
Point2D returnMaxPointV2(bool containterTypeSet)
{
Point2D maxPoint = { 0,0 };
std::set<Point2D> containerSet;
std::vector<Point2D> containerVector;
// specialized code
if (containterTypeSet) {
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
containerSet.insert(points[cnt]);
}
}
}
else{
for (int cnt = 0; cnt < 4; ++cnt) {
if (points[cnt].x2 >= 2) {
containerVector.push_back(points[cnt]);
}
}
}
std::set<Point2D>::iterator SetIterator;
std::vector<Point2D>::iterator VectorIterator;
const Point2D *ScopePoint;
bool foolCompiler; // the compiler was tripping if both types of the tenary operator (?:) were not of the same type, so I made the statement such that the statement was of type bool.
for(foolCompiler = containterTypeSet? ((SetIterator = containerSet.begin()) == SetIterator): ((VectorIterator = containerVector.begin()) == VectorIterator);
containterTypeSet ? SetIterator != containerSet.end() : VectorIterator != containerVector.end();
foolCompiler = containterTypeSet ? ((SetIterator++) == SetIterator) : ((VectorIterator++) == VectorIterator)
){
ScopePoint = containterTypeSet ? &(*SetIterator) : &(*VectorIterator);
// generic code
maxPoint = (maxPoint.x1 > ScopePoint->x1) ? maxPoint : *ScopePoint;
}
return maxPoint;
}
int main()
{
Point2D result;
result = returnMaxPointV1(true);
std::cout << "result1: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV1(false);
std::cout << "result2: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV2(true);
std::cout << "result3: (" << result.x1 << "," << result.x2 << ")" << std::endl;
result = returnMaxPointV2(false);
std::cout << "result4: (" << result.x1 << "," << result.x2 << ")" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(10));
}
程序的输出是:
result1: (10,2)
result2: (10,4)
result3: (10,2)
result4: (10,4)