为什么编译器说它看不到刚刚生成的模板?
Why does compiler say it can't see a template it has just generated?
如果您查看以下(编辑过的)编译器输出 (Visual C++ 2013),您会在第 5 行看到它告诉我它正在编译(我在预编译 header 中使用显式实例化):
TimeOnly IT_TimeOnly::operator ++<TimeOnly>(int).
这是 IT_TimeOnly
class 的模板化后缀运算符。
但是,在第 7 行,编译器警告找不到类型 IT_TimeOnly
的后缀运算符。谁能帮我理解为什么?
我的代码如下所示。
1>------ Build started: Project: TemplatedPostFix, Configuration: Debug Win32 ------
2> stdafx.cpp
3> Precompiled Header
4>c:...\it_timeonly.h(14): warning C4305: 'initializing' : truncation from 'int' to 'bool'
5> c:... \to_timeonly.h(22) : see reference to function template instantiation 'TimeOnly IT_TimeOnly::operator ++<TimeOnly>(int)' being compiled
6> Test_TemplatedPostFix.cpp
7>c:...\test_templatedpostfix.cpp(31): warning C4620: no postfix form of 'operator ++' found for type 'IT_TimeOnly', using prefix form
我的界面 class 带有模板化后缀:
typedef signed char period_t;
class IT_TimeOnly
{
public:
template<class T>
T operator++(int) {
bool b = 2; // here to generate warning for debugging
std::cout << "Template Post++\n";
return static_cast<T*>(this)->operator++(1);
}
virtual IT_TimeOnly & operator++() = 0;
virtual period_t getMins() const = 0;
virtual void setMins(period_t) = 0;
};
我的具体派生class:
class TimeOnly : public IT_TimeOnly
{
public:
TimeOnly(){};
TimeOnly(const IT_TimeOnly &);
TimeOnly(const TimeOnly &);
TimeOnly operator++(int);
TimeOnly & operator++() override {
cout << "Derived Pre++\n";
++mins;
return *this;
}
period_t getMins() const override;
void setMins(period_t mins) override;
private:
period_t hrs = 0;
period_t mins = 0;
};
我已经在 pre-compiled header 中明确实例化了 TimeOnly 的模板。这会产生第 3、4 和 5 行。
template TimeOnly IT_TimeOnly::operator++<TimeOnly>(int);
还有我的测试代码:
IT_TimeOnly & myBaseTime = myTime; // Base-class reference
myBaseTime++;
cout << (int)myBaseTime.getMins() << '\n';
// Prints:
// Derived Pre++
// 1
编译器无法从调用中推断出模板参数 T
。结果显式实例化失败。显式实例化的存在是无关紧要的,因为首先确定重载,然后查找或创建实例。但是,函数查找已经失败。
您似乎在尝试将基数 class 中的递增运算符定义为 return 派生 class 的对象。这对成员函数不起作用,除非您使用派生类型 (CRTP) 对基进行参数化。假设您适当地约束了模板,应该可以将运算符实现为非成员委托给合适的成员函数来执行适当的增量。
感谢@DietmarKühl 和@vsoftco 提供的有用答案。总结您的答案:...
所有调用模板参数不是参数类型的模板函数都必须使用模板参数限定,即使您要使用的版本已被显式实例化。原则上可以有多个不同类型的实例化,编译器必须知道选择哪个。
如果您查看以下(编辑过的)编译器输出 (Visual C++ 2013),您会在第 5 行看到它告诉我它正在编译(我在预编译 header 中使用显式实例化):
TimeOnly IT_TimeOnly::operator ++<TimeOnly>(int).
这是 IT_TimeOnly
class 的模板化后缀运算符。
但是,在第 7 行,编译器警告找不到类型 IT_TimeOnly
的后缀运算符。谁能帮我理解为什么?
我的代码如下所示。
1>------ Build started: Project: TemplatedPostFix, Configuration: Debug Win32 ------
2> stdafx.cpp
3> Precompiled Header
4>c:...\it_timeonly.h(14): warning C4305: 'initializing' : truncation from 'int' to 'bool'
5> c:... \to_timeonly.h(22) : see reference to function template instantiation 'TimeOnly IT_TimeOnly::operator ++<TimeOnly>(int)' being compiled
6> Test_TemplatedPostFix.cpp
7>c:...\test_templatedpostfix.cpp(31): warning C4620: no postfix form of 'operator ++' found for type 'IT_TimeOnly', using prefix form
我的界面 class 带有模板化后缀:
typedef signed char period_t;
class IT_TimeOnly
{
public:
template<class T>
T operator++(int) {
bool b = 2; // here to generate warning for debugging
std::cout << "Template Post++\n";
return static_cast<T*>(this)->operator++(1);
}
virtual IT_TimeOnly & operator++() = 0;
virtual period_t getMins() const = 0;
virtual void setMins(period_t) = 0;
};
我的具体派生class:
class TimeOnly : public IT_TimeOnly
{
public:
TimeOnly(){};
TimeOnly(const IT_TimeOnly &);
TimeOnly(const TimeOnly &);
TimeOnly operator++(int);
TimeOnly & operator++() override {
cout << "Derived Pre++\n";
++mins;
return *this;
}
period_t getMins() const override;
void setMins(period_t mins) override;
private:
period_t hrs = 0;
period_t mins = 0;
};
我已经在 pre-compiled header 中明确实例化了 TimeOnly 的模板。这会产生第 3、4 和 5 行。
template TimeOnly IT_TimeOnly::operator++<TimeOnly>(int);
还有我的测试代码:
IT_TimeOnly & myBaseTime = myTime; // Base-class reference
myBaseTime++;
cout << (int)myBaseTime.getMins() << '\n';
// Prints:
// Derived Pre++
// 1
编译器无法从调用中推断出模板参数 T
。结果显式实例化失败。显式实例化的存在是无关紧要的,因为首先确定重载,然后查找或创建实例。但是,函数查找已经失败。
您似乎在尝试将基数 class 中的递增运算符定义为 return 派生 class 的对象。这对成员函数不起作用,除非您使用派生类型 (CRTP) 对基进行参数化。假设您适当地约束了模板,应该可以将运算符实现为非成员委托给合适的成员函数来执行适当的增量。
感谢@DietmarKühl 和@vsoftco 提供的有用答案。总结您的答案:...
所有调用模板参数不是参数类型的模板函数都必须使用模板参数限定,即使您要使用的版本已被显式实例化。原则上可以有多个不同类型的实例化,编译器必须知道选择哪个。