Skip to content

Instantly share code, notes, and snippets.

@Axure
Last active April 9, 2017 15:48
Show Gist options
  • Select an option

  • Save Axure/ca03fd5a32cfc39f108a3bf0a5d55a26 to your computer and use it in GitHub Desktop.

Select an option

Save Axure/ca03fd5a32cfc39f108a3bf0a5d55a26 to your computer and use it in GitHub Desktop.

Revisions

  1. Hu Zheng revised this gist Apr 9, 2017. 1 changed file with 15 additions and 3 deletions.
    18 changes: 15 additions & 3 deletions rusty-c++.cpp
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,32 @@
    #include <iostream>

    struct MyTrait;
    struct AnotherTrait;
    struct YetAnotherTrait;
    struct RubbishTrait;

    template <class ... Traits>
    struct MyTypeWithTrait {};

    template< template<typename ...> class TypeWithTrait >
    void impledFunc(TypeWithTrait<MyTrait> entity) {
    template< template<typename ...> class TypeWithTrait, class ... OtherTraits >
    void impledFunc(TypeWithTrait<MyTrait, OtherTraits...> entity) {
    // do something.
    std::cout << "I'm fooed by C++\n";
    }

    template< template<typename ...> class TypeWithTrait, class HeadTrait, class ... OtherTraits >
    void impledFunc(TypeWithTrait<HeadTrait, OtherTraits...> entity) {
    // do something.
    impledFunc(*reinterpret_cast<MyTypeWithTrait<OtherTraits...>* >(&entity));
    }

    int main() {
    MyTypeWithTrait<MyTrait> myEntity;
    impledFunc(myEntity);

    MyTypeWithTrait<> myRubbishEntity;
    MyTypeWithTrait<YetAnotherTrait, AnotherTrait, MyTrait, RubbishTrait> myAnotherEntity;
    impledFunc(myAnotherEntity);

    MyTypeWithTrait<YetAnotherTrait, AnotherTrait, RubbishTrait> myRubbishEntity;
    impledFunc(myRubbishEntity); // Won't compile
    }
  2. Hu Zheng created this gist Apr 9, 2017.
    20 changes: 20 additions & 0 deletions rusty-c++.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    #include <iostream>

    struct MyTrait;

    template <class ... Traits>
    struct MyTypeWithTrait {};

    template< template<typename ...> class TypeWithTrait >
    void impledFunc(TypeWithTrait<MyTrait> entity) {
    // do something.
    std::cout << "I'm fooed by C++\n";
    }

    int main() {
    MyTypeWithTrait<MyTrait> myEntity;
    impledFunc(myEntity);

    MyTypeWithTrait<> myRubbishEntity;
    impledFunc(myRubbishEntity); // Won't compile
    }