-
Notifications
You must be signed in to change notification settings - Fork 1
/
item11_perfer_deleted_functions_to_private.cpp
43 lines (36 loc) · 1.23 KB
/
item11_perfer_deleted_functions_to_private.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include "type_name.hpp"
using namespace std;
bool isLucky(int number){
cout << "isLucky(int) : " << number;
return false;
}
// Disable all other kinds fo argumet.
template <typename T>
bool isLucky(T t) = delete;
//bool isLucky(char b) = delete;
// template specialization should be written at the namespace scope.
class Widget{
public:
template<typename T>
void processPointer(T* ptr){
}
private:
// In C++98, you want this kind of function should not work.
// But you will get the error :explicit specialization is not allowed in
// the current scope. And quotes from the Modern Effective C++ :
// the problem is that template specializations must be written at namespace scope,
// not class scope. Which means that you can not use this to avoid invoking
// void processPointer<void>(void *) {}. Because that, template specialization
// only occurs outside of class, which means it can not define in the private part.
// template<> void processPointer<void>(void *){}
};
// The delete keyword does not be affected. like this
template<>
void Widget::processPointer<void>(void*) = delete;
int main(){
isLucky(1);
//isLucky('1');
// isLucky('1');
return 0;
}