gpt4 book ai didi

c++ - 如何将 enable_if 用于模板类成员的外联定义

转载 作者:行者123 更新时间:2023-12-01 14:43:27 26 4
gpt4 key购买 nike

我试图了解 enable_if 的用法,但我在这方面确实遇到了一些困难。在这里,我编写了一个测试代码,但似乎没有按预期工作。

#include <iostream>

template <typename T>
class Base{
public:
template <typename U>
U Compute(U a, U b);
};

using AddOperation = Base<int>;

template<>
template<typename U>
typename std::enable_if<!std::is_same<U, bool>::value, U>::type
AddOperation::Compute(U a, U b){
return a + b;
}

int main(){
Base<int> b;
std::cout << b.Compute<int>(10, 2) << std::endl;
std::cout << b.Compute<bool>(true, false) << std::endl;
return 0;
}

意图:不想为 bool 类型启用计算

但在上面的代码中,它是有效的。如何确保 bool 的 Compute 函数未被编译器特化?

编辑1

最终目标是为 T=T1 的 U=bool 启用 Compute 并为 T=T2 的 U=bool 禁用 Compute。这是我试图实现相同目标的另一个示例代码

#include <iostream>

enum class OpType{
INT,
BITWISE,
};

template <OpType T>
class Base{
public:
template <typename U>
typename std::enable_if<!std::is_same<U, bool>::value, U>::type
Compute(U a, U b);
};

using AddOperation = Base<OpType::INT>;

template<>
template<typename U>
typename std::enable_if<!std::is_same<U, bool>::value, U>::type
AddOperation::Compute(U a, U b){
std::cout << a << "," << b << std::endl;
return a + b;
}

using AndOperation = Base<OpType::BITWISE>;

template<>
template<typename U>
typename std::enable_if<std::is_same<U, bool>::value, U>::type
AndOperation::Compute(U a, U b){
return a & b;
}

int main(){
AddOperation b;
AndOperation a;
std::cout << b.Compute<int>(10, 2) << std::endl;
std::cout << a.Compute<bool>(true, true) << std::endl;
return 0;
}

最佳答案

您也应该像定义那样在声明中使用 enable_if

template <typename T>
class Base{
public:
template <typename U>
typename std::enable_if<!std::is_same<U, bool>::value, U>::type Compute(U a, U b);
};

LIVE

事实上,clang如我所料拒绝您当前的代码,因为声明和定义不匹配。

error: out-of-line definition of 'Compute' does not match any declaration in 'Base'


编辑(针对您添加的问题)

你可以

template <OpType T>
class Base{
public:
template <typename U, OpType X = T>
typename std::enable_if<
(X == OpType::INT && !std::is_same<U, bool>::value)
||
(X == OpType::BITWISE && std::is_same<U, bool>::value), U
>::type
Compute(U a, U b);
};

using AddOperation = Base<OpType::INT>;

template<>
template<typename U, OpType X>
typename std::enable_if<
(X == OpType::INT && !std::is_same<U, bool>::value)
||
(X == OpType::BITWISE && std::is_same<U, bool>::value), U
>::type
AddOperation::Compute(U a, U b){
std::cout << a << "," << b << std::endl;
return a + b;
}

using AndOperation = Base<OpType::BITWISE>;

template<>
template<typename U, OpType X>
typename std::enable_if<
(X == OpType::INT && !std::is_same<U, bool>::value)
||
(X == OpType::BITWISE && std::is_same<U, bool>::value), U
>::type
AndOperation::Compute(U a, U b){
return a & b;
}

然后

std::cout << b.Compute<int>(10, 2) << std::endl;       // fine
std::cout << a.Compute<bool>(true, true) << std::endl; // fine
std::cout << b.Compute<bool>(true, true) << std::endl; // error, no matching function
std::cout << a.Compute<int>(10, 2) << std::endl; // error, no matching function

LIVE

另一种方法是 class template specialization , 以分离 OpType::INTOpType::BITWISE 的实现。

关于c++ - 如何将 enable_if 用于模板类成员的外联定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60181772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com