我正在使用启用了C ++ 17支持的VS2017。
我想尝试创建一个“Transformer”类,当它提供某种类型的支持时,它将转换类型,否则它将按原样返回变量。目标是将所有变量类型传递给变换器,并“隐藏”其变换的变量。这样调用者可以尝试转换所有内容,而不必担心转换是否必要,变换器会知道。
一个更完整的例子(从原文编辑):
class MyPoint
{
public:
int x = 0;
};
class NotMyPoint
{
public:
int x = 50;
};
template <typename T>
class ITransform
{
public:
virtual ~ITransform() {};
virtual T InTransform(const T &in) const = 0;
virtual T OutTransform(const T &out) const = 0;
//Check if the argument type is the same as this class type
template <typename X>
constexpr bool CanTransform() const
{
return std::is_same<X, T>::value;
}
};
class MyTransformer :
public ITransform<MyPoint>
{
public:
MyTransformer() = default;
virtual MyPoint InTransform(const MyPoint &in) const override
{
auto newPt = in;
newPt.x += 100;
return newPt;
}
virtual MyPoint OutTransform(const MyPoint &in) const override
{
auto newPt = in;
newPt.x -= 100;
return newPt;
}
};
template <class... TRANSFORMERS>
struct VariadicTransformer
{
constexpr VariadicTransformer() = default;
/** \brief parse using validateParse but catch throw */
template <typename T>
inline T Transform(const T& in)
{
return TransformImpl<sizeof...(TRANSFORMERS)-1, T>(in);
}
private:
/// last attempt to find matching transformer at I==0, if it fails return the original value
template<std::size_t I = 0, typename T>
inline typename std::enable_if<I == 0, T>::type TransformImpl(const T &in) const
{
if (std::get<I>(transformers).CanTransform<T>())
return std::get<I>(transformers).InTransform(in);
else
return in;
}
/// attempt to find transformer for this type
template<std::size_t I = 0, typename T>
inline typename std::enable_if < I != 0, T>::type TransformImpl(const T &in) const
{
if (std::get<I>(transformers).CanTransform<T>())
return std::get<I>(transformers).InTransform(in);
else
return TransformImpl<I - 1, T>(in);
}
std::tuple<const TRANSFORMERS...> transformers;
};
//Example usage
VariadicTransformer<MyTransformer, MyTransformer> varTrans;
MyPoint myPoint;
NotMyPoint notMyPoint;
std::cout << myPoint.x << std::endl;
myPoint = varTrans.Transform(myPoint);
std::cout << myPoint.x << std::endl;
std::cout << notMyPoint.x << std::endl;
notMyPoint = varTrans.Transform<NotMyPoint>(notMyPoint);
std::cout << notMyPoint.x << std::endl;
return 0;
我的问题出现在这一行:
if constexpr(std::get<I>(transformers).CanTransform<T>())
这将无法编译并提供以下错误:
错误C2131:表达式未评估为常量
注意:失败是由在其生命周期之外读取变量引起的
注意:请参阅'this'的用法
CanTransform函数应该是constexpr,std :: get&lt;#&gt;(std :: tuple)应该是constexpr,所以我不确定它对这一行的抱怨是什么。
此外,如果要求constexpr避免尝试调用任何不适合转换当前类型的变换器,我希望此案例能够通过并返回原始类型。
有关导致此错误的原因或我可以尝试的其他设计的任何建议吗?
答案 0 :(得分:3)
如果调用对象也 constexpr
,则对象的方法调用只能是constexpr
。如果调用对象不是constexpr
,那么该方法仍将在运行时而不是编译时进行评估,因此不适用于任何编译时评估。
struct A {
int val;
constexpr A() : A(16) {}
constexpr A(int val) : val(val) {}
constexpr bool foo() const {return val > 15;}
};
int main() {
A a;
if constexpr(a.foo()) {
std::cout << "No point thinking about it; this won't compile!" << std::endl;
} else {
std::cout << "Again, the previous line doesn't compile." << std::endl;
}
constexpr A b;
if constexpr(b.foo()) {
std::cout << "This, however, will compile, and this message will be displayed!" << std::endl;
}
constexpr A c(13);
if constexpr(c.foo()) {
std::cout << "This will not be displayed because the function will evaluate to false, but it will compile!" << std::endl;
}
}
您需要确保TransformImpl
可以constexpr
,然后确保调用A
的{{1}}实例也是TransformImpl
。
答案 1 :(得分:0)
所以我无法解释为什么以前的选项不起作用。但是,通过将CanTransform函数变为自己的独立函数,错误就消失了。
这最终有效:
template<typename X, typename T>
constexpr inline bool CanTransform()
{
return std::is_base_of<ITransform<T>, X>::value;
}
template <class... TRANSFORMERS>
struct VariadicTransformer
{
constexpr VariadicTransformer() = default;
template <typename T>
constexpr inline T Transform(const T& in) const
{
return TransformImpl<sizeof...(TRANSFORMERS)-1, T>(in);
}
private:
// last attempt to find matching transformer at I==0, if it fails return the original value
template<std::size_t I = 0, typename T>
constexpr inline typename std::enable_if<I == 0, T>::type TransformImpl(const T &in) const
{
if constexpr(CanTransform < std::tuple_element < I, std::tuple<TRANSFORMERS...>>::type, T >())
return std::get<I>(transformers).InTransform(in);
else
return in;
}
// attempt to find transformer for this type
template<std::size_t I = 0, typename T>
constexpr inline typename std::enable_if < I != 0, T>::type TransformImpl(const T &in) const
{
if constexpr(CanTransform < std::tuple_element < I, std::tuple<TRANSFORMERS...>>::type, T >())
return std::get<I>(transformers).InTransform(in);
else
return TransformImpl<I - 1, T>(in);
}
std::tuple<TRANSFORMERS...> transformers;
};