gpt4 book ai didi

c++如何为类的模板化转换运算符指定参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:18:35 24 4
gpt4 key购买 nike

我正在尝试为类的模板化转换运算符指定模板参数,但我似乎无法获得正确的语法。

#include <iostream>
using namespace std;

class C
{
int i_;
public:
C(int i) : i_(i) {}
template<int adder> int get() { return i_ + adder; }
template<int adder> int operator()() { return i_ + adder; }
template<int adder> operator int() { return i_ + adder; }
// If I add a default argument to operator int()'s adder template parameter this compiles fine
// (of course, I still can't figure out how to specify it...)
};

int main(int, char*[])
{
C c(10);
cout << c.get<2>() << endl; // I can specify template argument here the regular way.
// cout << c() << endl; // No template argument specified, so I wouldn't have expected this to work.
cout << c.operator()<3>() << endl; // We have to call it this way.
// cout << (int)c << endl; // In the same vein I wouldn't expect this to work either.
cout << c.operator int<4>() << endl; // But how do I specify template argument here? This seems to be an error for some compilers.
return 0;
}

相同的代码在 http://liveworkspace.org/code/35sqXe$4

使用 g++ 4.7.2 编译时

$ g++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"

Compilation finished with errors:
source.cpp: In function 'int main(int, char**)':
source.cpp:23:23: error: 'int' is not a template
source.cpp:23:30: error: no matching function for call to 'C::operator int()'
source.cpp:23:30: note: candidate is:
source.cpp:11:24: note: template<int adder> C::operator int()
source.cpp:11:24: note: template argument deduction/substitution failed:
source.cpp:23:30: note: couldn't deduce template parameter 'adder'

使用 g++ 4.8.0 (20130224) 编译时

$ g++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"

Compilation finished with errors:
source.cpp: In function 'int main(int, char**)':
source.cpp:23:23: error: 'int' is not a template
cout << c.operator int<4>() << endl;
^
source.cpp:23:30: error: no matching function for call to 'C::operator int()'
cout << c.operator int<4>() << endl;
^
source.cpp:23:30: note: candidate is:
source.cpp:11:24: note: template<int adder> C::operator int()
template<int adder> operator int() { return i_ + adder; }
^
source.cpp:11:24: note: template argument deduction/substitution failed:
source.cpp:23:30: note: couldn't deduce template parameter 'adder'
cout << c.operator int<4>() << endl;
^

使用 clang++ 3.2 编译时

$ clang++ -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"

Compilation finished with errors:
source.cpp:23:12: error: reference to non-static member function must be called
cout << c.operator int<4>() << endl;
^~~~~~~~~~~~~~
source.cpp:23:30: error: expected expression
cout << c.operator int<4>() << endl;
^
2 errors generated.

使用 icc 13.0.1 编译时

$ icc -std=c++11 -Wall -W -pedantic "template conversion operator.cpp"

Compilation finished with warnings:
source.cpp(11): warning #488: constant "adder" is not used in declaring the parameter types of function template "C::operator int"
template<int adder> operator int() { return i_ + adder; }
^

除警告外,icc 似乎工作正常。

这些是编译器错误吗?还是我的语法有问题?

编辑

自从 Yakk 问我原来/实际的问题是什么:
我有一个 Ptr 类(以它指向的类型为模板),我想将 Ptr 转换为 const T。(虽然我知道在这种情况下这并不重要,)我希望转换运算符不如果 T 已经是 const 类型,就在那里。由于您没有为转换运算符指定返回类型或方法参数,因此我将 enable_if 作为方法模板参数的一部分。

正如 Yakk(和其他问题中的其他人)发布的那样,一个简单的 template <typename = typename std::enable_if<!std::is_const<T>::value>::type>不起作用,因为当实例化 Ptr 时,T 在编译器到达此声明时已知。由于没有推导 T,因此不存在 SFINAE。因为我们知道!is_const<T>::value为假,没有“类型”成员,声明无效。使模板依赖于新类型 (U),推导 U,然后检查 U 是否与 T 相同,以及 T 不是 const,然后进行无效声明是 SFINAE 的有效使用并且有效正如预期的那样。

template <typename T>
class Ptr
{
template <typename U,
typename = typename std::enable_if<std::is_same<T, U>::value &&
!std::is_const<U>::value>::type>
operator Ptr<const U>() const { return active; }
};

但后来我对自己说,这是一个模板化的成员函数。这些模板参数不必保留为默认值,任何实例化该函数的人都可以指定它们。对于任何其他 operator xxx 函数,执行此操作的语法很明显并且有效(请参阅上面的 operator())。对于这个例子:

Ptr<const int> ci;
ci.operator Ptr<const int><const int, void>(); // assuming this syntax is valid

void(或那里的任何其他类型)将指定转换运算符的第二个模板参数,并且不会考虑包含 enable_if 的默认值。当我试图使它不存在时,这将使该方法存在。

但是 gcc、clang 和 msvc 似乎对这种语法有问题。我假设因为转换运算符拼写为 operator typename ,具有模板参数会使编译器感到困惑,认为它们是用于类型名而不是运算符。

确实有变通办法(只包括转换运算符,当 T 已经是 const 时转换为 const T 不会造成任何伤害),但那是针对这个特定问题的。也许无法为转换运算符指定模板参数,因此,让这些类型被推导/默认就可以了。或者也许它有一个语法(icc 似乎接受它......),所以我向用户开放自己指定模板参数和实例化我不想要它们的方法。我已经有了针对我的特定问题的解决方案(在类型确实重要的时候,在转换运算符的类型检查中使用 static_assert),但这个问题是关于 C++ 语言及其语法的。顶部的 C 类只是我能想到的搜索该语法的最简单方法。

最佳答案

有点不清楚您要实现的目标...通常没有充分的理由让所有这些成员函数成为模板,您也可以将它们设为采用 adder 的常规函数​​。作为论点。

get函数,好吧,并不是真正的获取,而是添加,所以你可以称它为add .函数调用运算符 operator()()很可能需要一个int作为论据。转换运算符为 int从字面上看,作为模板没有任何意义,并且不能按定义调用。如果你坚持要 getoperator()作为模板,您可以将它们称为:

C c(0);
c.get<5>(); // 5
c<5>(); // 5

但我建议您重新考虑设计,确定您真正需要什么以及模板是否可行...(请注意,即使在非模板版本中,转换为int 采用,您不是转换,而是创建一个不同的 int !)

关于c++如何为类的模板化转换运算符指定参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15305891/

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