WEBVTT

00:00.120 --> 00:00.660
Hello again!

00:00.870 --> 00:05.850
In this video, we are going to look at inline namespaces. In C++,

00:05.850 --> 00:08.430
you can define one namespace inside another.

00:09.030 --> 00:13.440
So we have a namespace A, and inside that we have another namespace B.

00:14.160 --> 00:19.080
So these are "nested" namespaces. If we have any symbols in here,

00:19.260 --> 00:22.800
so functions, classes, variables and so on.

00:23.640 --> 00:29.430
If we want to use them outside the namespace, we have to put the namespace double colon in front.

00:30.150 --> 00:36.720
So if we are using x in this namespace, we have to put B double colon x, because it is defined in

00:36.810 --> 00:43.160
a namespace B inside this namespace. If we are outside any namespace, then we need to put another A

00:43.200 --> 00:44.490
double colon in front of that.

00:44.940 --> 00:48.210
So, A double colon B to put code on x.

00:50.900 --> 00:55.850
In C++11, we can say that the inner namespace is "inline".

00:56.330 --> 00:59.780
So namespace B is inline to namespace A.

01:00.710 --> 01:06.950
The difference is that any symbols which are defined inside the inline namespace B, will appear

01:06.950 --> 01:08.210
to be defined in A.

01:08.750 --> 01:13.220
So if we want to use anything from B in this namespace, we can just use it.

01:13.220 --> 01:15.470
We do not need to put B double colon.

01:16.490 --> 01:22.490
If we are outside the namespace, then this will appear to be defined in A. So we can just put A double

01:22.490 --> 01:23.150
colon x.

01:23.510 --> 01:26.300
And again, we do not need to put the B double colon.

01:27.740 --> 01:36.770
And, just to prove that is allowed. This is used in the C++ standard library. In C++14, there are literals

01:36.770 --> 01:37.640
for strings

01:37.640 --> 01:44.990
and chrono. If we put "s" after a C-style string, that makes it into a C++ string literal.

01:45.770 --> 01:52.490
If we put "s", "h" or "n" after a number, that makes a seconds, hours or minutes literal. These have

01:52.490 --> 01:59.390
their own namespaces, which are inside the literals namespace, which is inside the std namespace.

02:02.160 --> 02:03.150
The string literals

02:03.150 --> 02:06.720
chrono literals are inline to literals.

02:07.140 --> 02:12.930
So if we put "using namespace std::literals", that will bring in all the symbols from string_literals

02:12.930 --> 02:14.110
and chrono_literals.

02:15.210 --> 02:18.180
The literals namespace in turn, is inline to std.

02:18.630 --> 02:23.130
So if we have "using namespace std", which you should not really do unless you are writing toy

02:23.130 --> 02:26.580
programs, that will bring in everything from literals automatically.

02:29.710 --> 02:30.310
Inline

02:30.310 --> 02:35.110
namespaces are also useful if you are developing applications, if you are working with different versions

02:35.110 --> 02:35.560
of code.

02:36.640 --> 02:40.270
We have been occasionally using the Refrigerator class during this course.

02:40.630 --> 02:46.330
We have two versions, one with basic functionality, and an enhanced one which can connect to the Internet.

02:46.930 --> 02:49.960
So let's say we are writing software for refrigerators.

02:50.470 --> 02:53.290
We have a "product" name space where we write all our code.

02:54.100 --> 02:55.630
Then we have two different namespaces.

02:55.990 --> 03:01.810
The "Version1" namespace is for the basic Refrigerator, and the Version2 namespace is for the

03:01.810 --> 03:03.430
enhanced Refrigerator.

03:03.850 --> 03:06.280
And these are both inside the product namespace.

03:08.500 --> 03:09.820
So we have something like this.

03:09.850 --> 03:13.790
This is the product namespace, and we are writing all our code in it.

03:14.800 --> 03:20.260
We have the Version1 and Version2 namespaces which both have a definition of Refrigerator.

03:21.190 --> 03:26.860
If we make the Version1 namespace inline, then the Version1 definition will appear

03:26.860 --> 03:28.690
to be inside the "product" namespace.

03:29.200 --> 03:34.720
So if we just put Refrigerator without any qualification, this will pick up the basic Refrigerator

03:34.720 --> 03:35.290
definition.

03:37.340 --> 03:42.860
If instead, we have Version2 inline, then this class definition will appear to be in the product

03:42.920 --> 03:47.150
namespace, and we will pick up the enhanced Refrigerator.

03:50.240 --> 03:52.400
So you can use this for working with versions.

03:52.910 --> 03:58.160
What you do is, you put the code for the current version in an inline namespace, inside the product

03:58.160 --> 03:58.700
namespace.

03:59.330 --> 04:02.810
The code for the other versions goes in nested namespaces.

04:04.180 --> 04:09.370
If we use symbols without any scope operators, we automatically get the current version.

04:10.090 --> 04:14.800
If we want to use a different version for some reason, then we need to qualify it with the namespace

04:14.800 --> 04:18.190
for that version. And then, later on,

04:18.190 --> 04:23.290
when a new version is released, we make the namespace for that version inline, and the previous version

04:23.290 --> 04:25.480
is changed to a nested namespace.

04:27.340 --> 04:33.010
And all our code will automatically start using the new version unless it has explicit scope operators,

04:33.370 --> 04:37.210
in which case it will continue using the same version it was using before.

04:40.100 --> 04:41.360
So let's try this out.

04:41.390 --> 04:45.560
Here is the Version1 namespace, for the basic Refrigerator class.

04:46.490 --> 04:51.320
The Version2 has the enhanced Refrigerator which can connect to the internet.

04:51.740 --> 04:54.740
This is all code you have seen before, so it is not really important what it does.

04:56.150 --> 04:57.740
And then our product code.

04:58.010 --> 05:01.460
So we write all our code in the product namespace.

05:02.090 --> 05:03.920
We include the headers for both versions.

05:05.470 --> 05:08.470
At the moment, Version1 is defined as inline.

05:09.340 --> 05:16.090
So if we just use Refrigerator without any namespace qualification, this will pick up the Version1

05:16.360 --> 05:18.370
namespace. The basic Refrigerator.

05:21.480 --> 05:21.950
And there we are.

05:21.960 --> 05:23.550
That is the basic Refrigerator.

05:25.560 --> 05:26.520
If we now change this.

05:26.520 --> 05:32.850
So that Version1 becomes a normal, nested namespace and Version2 becomes inline.

05:35.700 --> 05:37.320
So this is inline in the product.

05:38.780 --> 05:39.230
(Better save that)

05:41.090 --> 05:47.060
So now if we use Refrigerator without any qualification, that is going to pick up the Version2 definition,

05:47.600 --> 05:49.250
and we should see it connecting to the Internet.

05:49.850 --> 05:50.470
And there it is.

05:51.050 --> 05:52.940
And please do not hack into my fridge!

05:53.810 --> 05:54.110
Okay.

05:54.110 --> 05:55.220
So that is it for this video.

05:55.610 --> 05:56.450
I will see you next time.

05:56.630 --> 05:58.660
Until then, keep coding!
