gpt4 book ai didi

c++ - 如何让成员函数实现依赖于类的模板参数?

转载 作者:搜寻专家 更新时间:2023-10-31 02:06:54 25 4
gpt4 key购买 nike

<分区>

这是我最好的尝试:

#include <iostream>

template <bool EnableSomething = false>
class MyClass
{
typename std::enable_if< EnableSomething >::type
something(int& x)
{
x += 1; //do something
}

typename std::enable_if< !EnableSomething >::type
something(int& x)
{
// do nothing, should be optimized away
}

public:
void Process()
{
int x = 0;
something(x);
std::cout << "Enabled: " << EnableSomething << ". x = " << x << std::endl;
}
};

int main()
{
MyClass<true> yes;
MyClass<false> no;
yes.Process();
no.Process();
return 0;
}

编译器说: tester.cpp(12): error C2039: 'type': is not a member of 'std::enable_if<false,_Ty>'

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