Skip to content

Commit

Permalink
Fixed typos in document
Browse files Browse the repository at this point in the history
  • Loading branch information
wqking committed Dec 11, 2023
1 parent cb46e65 commit dbdbfb3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
19 changes: 15 additions & 4 deletions doc/tutorial/tutorial_variant.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ v = std::string("hello");
```

Get as std::string will copy the value, that's inefficient.
We should get as reference.

```c++
ASSERT(v.get<std::string>() == "hello");
```
Get as reference to avoid copy.
We should get as reference to avoid copy.
```c++
ASSERT(v.get<const std::string &>() == "hello");
Expand All @@ -63,6 +62,11 @@ Whether the reference is const, it doesn't matter.

```c++
ASSERT(v.get<std::string &>() == "hello");
```
We can assign to the reference if it's not const.
```c++
v.get<std::string &>() = "world";
ASSERT(v.get<const std::string &>() == "world");
```
Expand All @@ -77,7 +81,14 @@ ASSERT(strcmp(v.get<const char []>(), "great") == 0);
Cast to const char *.
```c++
metapp::Variant casted = v.cast<std::string>();
metapp::Variant casted = v.cast<const char *>();
ASSERT(strcmp(casted.get<const char *>(), "great") == 0);
```

Cast to std::string.

```c++
casted = v.cast<std::string>();
ASSERT(casted.get<const std::string &>() == "great");
```
Expand All @@ -89,7 +100,7 @@ int array[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };

Now v contains reference to int[2][3].
We can't simply assign array to v because the array type will be lost.
We need to call Variant::create to retain the array type.
We need to call Variant::create or Variant::reference to retain the array type.

```c++
v = metapp::Variant::reference(array);
Expand Down
4 changes: 2 additions & 2 deletions doc/variant.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ This function is not useful in most cases. The only useful case is to use it on
**Example**

```c++
// This is wrong. In this case, var hold the ownership of MyClass
// This is wrong. In this case, var hold the ownership of std::string
// takeFrom can't take the ownership from var
//metapp::Variant var(std::string());

// This works, var hold the pointer, it doesn't hold the ownership of MyClass
// This works, var hold the pointer, it doesn't hold the ownership of std::string
// Note: if we don't call takeFrom later, var will leak the object
// because it doesn't hold the ownership.
metapp::Variant var(new std::string());
Expand Down
4 changes: 2 additions & 2 deletions tests/docsrc/doc_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ desc*/
ExampleFunc
{
//code
// This is wrong. In this case, var hold the ownership of MyClass
// This is wrong. In this case, var hold the ownership of std::string
// takeFrom can't take the ownership from var
//metapp::Variant var(std::string());

// This works, var hold the pointer, it doesn't hold the ownership of MyClass
// This works, var hold the pointer, it doesn't hold the ownership of std::string
// Note: if we don't call takeFrom later, var will leak the object
// because it doesn't hold the ownership.
metapp::Variant var(new std::string());
Expand Down
12 changes: 8 additions & 4 deletions tests/docsrc/tutorial/doc_tutorial_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ TEST_CASE("tutorialVariant")
//desc Now v contains std::string.
v = std::string("hello");
//desc Get as std::string will copy the value, that's inefficient.
//desc We should get as reference.
ASSERT(v.get<std::string>() == "hello");
//desc Get as reference to avoid copy.
//desc We should get as reference to avoid copy.
ASSERT(v.get<const std::string &>() == "hello");
//desc Whether the reference is const, it doesn't matter.
ASSERT(v.get<std::string &>() == "hello");
//desc We can assign to the reference if it's not const.
v.get<std::string &>() = "world";
ASSERT(v.get<const std::string &>() == "world");

Expand All @@ -67,14 +67,18 @@ TEST_CASE("tutorialVariant")
ASSERT(strcmp(v.get<const char []>(), "great") == 0);

//desc Cast to const char *.
metapp::Variant casted = v.cast<std::string>();
metapp::Variant casted = v.cast<const char *>();
ASSERT(strcmp(casted.get<const char *>(), "great") == 0);

//desc Cast to std::string.
casted = v.cast<std::string>();
ASSERT(casted.get<const std::string &>() == "great");

//desc Let's see how Variant works with array
int array[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
//desc Now v contains reference to int[2][3].
//desc We can't simply assign array to v because the array type will be lost.
//desc We need to call Variant::create to retain the array type.
//desc We need to call Variant::create or Variant::reference to retain the array type.
v = metapp::Variant::reference(array);
ASSERT(v.get<int (&)[2][3]>()[1][2] == 6);
//desc Since v is a reference to array, modify array will also modify v
Expand Down

0 comments on commit dbdbfb3

Please sign in to comment.