@@ -494,6 +494,7 @@ constructible from ``nullptr`` and that results in a value you can tell is
494
494
invalid.
495
495
496
496
.. code-block :: c++
497
+
497
498
class SomeValue {
498
499
public:
499
500
SomeValue(void *ptr) : ptr(ptr) {}
@@ -509,6 +510,7 @@ now, we assume that the types we want to cast *to* all provide ``classof``. So
509
510
we can use some provided cast traits like so:
510
511
511
512
.. code-block :: c++
513
+
512
514
template<typename T>
513
515
struct CastInfo<T, SomeValue>
514
516
: public CastIsPossible<T, SomeValue>,
@@ -525,6 +527,7 @@ Now given the value above ``SomeValue``, maybe we'd like to be able to cast to
525
527
that type from a char pointer type. So what we would do in that case is:
526
528
527
529
.. code-block :: c++
530
+
528
531
template<typename T>
529
532
struct CastInfo<SomeValue, T *>
530
533
: public NullableValueCastFailed<SomeValue>,
@@ -547,6 +550,7 @@ way to tell when an object is invalid, you may want to use ``llvm::Optional``.
547
550
In those cases, you probably want something like this:
548
551
549
552
.. code-block :: c++
553
+
550
554
template<typename T>
551
555
struct CastInfo<T, SomeValue>
552
556
: public OptionalValueCast<T, SomeValue> {};
@@ -555,12 +559,14 @@ That cast trait requires that ``T`` is constructible from ``const SomeValue &``
555
559
but it enables casting like so:
556
560
557
561
.. code-block :: c++
562
+
558
563
SomeValue someVal = ...;
559
564
Optional<AnotherValue> valOr = dyn_cast<AnotherValue>(someVal);
560
565
561
566
With the ``_is_present `` variants, you can even do optional chaining like this:
562
567
563
568
.. code-block :: c++
569
+
564
570
Optional<SomeValue> someVal = ...;
565
571
Optional<AnotherValue> valOr = dyn_cast_if_present<AnotherValue>(someVal);
566
572
0 commit comments