C ++中是否可以确定变量的类型是指针还是任何带有重载operator*
的迭代器?
有标准的std::is_pointer
,但它没有说明迭代器。
我想在这样的代码中使用它:
template<class T>
void func(T var)
{
if constexpr (can_be_dereferenced<T>::value)
{
// do something with *var;
}
}
答案 0 :(得分:3)
您基本上想检查Sub Delete_EntireColumn()
Dim LCol as Long, LRow as Long, i as Long
With Workbooks(REF).Sheets(REF)
LCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = LCol To 1 Step - 1
LRow = Application.WorksheetFunction.CountA(.Columns(i))
If LRow < 30 Then
.Columns(i).EntireColumn.Delete
End If
Next i
End With
End Sub
是否有效。这是SFINAE检查的理想用例:
*var
答案 1 :(得分:0)
您可以使用编译时函数重载分辨率和SFINAE来做到这一点:
template<class T, typename = std::enable_if_t<std::is_pointer<T>::value>>
void func(T var)
{
// a pointer
}
template<class T, typename = std::enable_if_t<!std::is_pointer<T>::value>,
typename = T::value_type>
void func(T var)
{
// possibly an iterator
}
int main()
{
int *i = new int(11);
func(i); // Calls the first overload
std::vector<int> v;
std::vector<int>::const_iterator it = v.begin();
func(it); // Calls the second overload
func(2); // Fail, as there is no function for this argument.
[..]