File tree Expand file tree Collapse file tree 5 files changed +80
-0
lines changed
Expand file tree Collapse file tree 5 files changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " engine.h"
2+
3+ namespace engine {
4+
5+ template <>
6+ struct specs <short > {
7+ int power () const { return 100 ; }
8+ };
9+
10+ template <>
11+ struct specs <long > {
12+ int power () const { return 200 ; }
13+ };
14+
15+ } // namespace engine
16+
17+ int short_car_power () { return engine::power<short >(); }
18+ int long_car_power () { return engine::power<long >(); }
Original file line number Diff line number Diff line change 1+ set -e
2+ set -x
3+ clang++ -std=c++11 car.cc truck.cc vehicle_specs.cc && a.out
4+ clang++ -std=c++11 truck.cc car.cc vehicle_specs.cc && a.out
5+ clang++ -std=c++11 -DUSE_TRANSLATION_UNIT_TAG car.cc truck.cc vehicle_specs.cc && a.out
6+ clang++ -std=c++11 -DUSE_TRANSLATION_UNIT_TAG truck.cc car.cc vehicle_specs.cc && a.out
7+ rm a.out
Original file line number Diff line number Diff line change 1+ namespace engine {
2+
3+ #ifdef USE_TRANSLATION_UNIT_TAG
4+ namespace {
5+ struct translation_unit_tag {};
6+ } // namespace
7+
8+ template <typename T, typename = translation_unit_tag>
9+ #else
10+ template <typename T>
11+ #endif
12+ struct specs ;
13+
14+ #ifdef USE_TRANSLATION_UNIT_TAG
15+ template <typename T, typename = translation_unit_tag>
16+ #else
17+ template <typename T>
18+ #endif
19+ int power () {
20+ return specs<T>().power ();
21+ }
22+
23+ } // namespace engine
Original file line number Diff line number Diff line change 1+ #include " engine.h"
2+
3+ namespace engine {
4+
5+ template <>
6+ struct specs <short > {
7+ int power () const { return 300 ; }
8+ };
9+
10+ template <>
11+ struct specs <long > {
12+ int power () const { return 400 ; }
13+ };
14+
15+ } // namespace engine
16+
17+ int short_truck_power () { return engine::power<short >(); }
18+ int long_truck_power () { return engine::power<long >(); }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+
3+ int short_car_power ();
4+ int long_car_power ();
5+ int short_truck_power ();
6+ int long_truck_power ();
7+
8+ int main () {
9+ std::cout << " short car power: " << short_car_power () << std::endl;
10+ std::cout << " long car power: " << long_car_power () << std::endl;
11+ std::cout << " short truck power: " << short_truck_power () << std::endl;
12+ std::cout << " long truck power: " << long_truck_power () << std::endl;
13+ return 0 ;
14+ }
You can’t perform that action at this time.
0 commit comments