Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename T> struct Class
- {
- int i;
- void foo()
- {
- i = 5;
- std::cout << "Generic implementation" << std::endl;
- }
- };
- template <> void Class<int>::foo()
- {
- i = 42;
- std::cout << "Specialized implementation" << std::endl;
- }
- int main()
- {
- Class<double> d;
- d.foo();
- std::cout << d.i << std::endl;
- Class<int> i;
- i.foo();
- std::cout << i.i << std::endl;
- }
- #include <iostream>
- #include <string>
- using namespace std;
- template <class T>
- struct A
- {
- A(T x) : x(x) {}
- void foo() = delete; // { cout << "default: " << x << endl; }
- void bar() { cout << "bar: " << x << endl; }
- T x;
- };
- template<>
- void A<int>::foo()
- {
- cout << "int: " << x << endl;
- }
- int main()
- {
- A<int> a(0);
- a.foo();
- a.bar();
- A<double> b(0);
- // b.foo(); // - не компилируется
- b.bar();
- A<string> c("hello");
- // c.foo(); // - не компилируется
- c.bar();
- return 0;
- }
Add Comment
Please, Sign In to add comment