Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Template dependant argument

Template dependant argument

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 3 Posters 1.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kofr
    wrote on last edited by Kofr
    #1
    #include <iostream>
    
    template<class T1>
    struct MyStruct {
        typedef T1 type;
        T1 data;
    }
    
    int main()
    {
    MyStruct<int> myData;
        MyStruct<myData::type> myDataTyped;
    }
    

    how to make MyStruct<myData::type> myDataTyped; to work?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi!

      template<class T1>
      struct MyStruct {
          using t1_type = T1;
          T1 data;
      };
      
      // ...
      
      MyStruct<int> myData;
      MyStruct<decltype(myData)::t1_type> myDataTyped;
      
      myDataTyped.data = 666;
      
      K 1 Reply Last reply
      5
      • ? A Former User

        Hi!

        template<class T1>
        struct MyStruct {
            using t1_type = T1;
            T1 data;
        };
        
        // ...
        
        MyStruct<int> myData;
        MyStruct<decltype(myData)::t1_type> myDataTyped;
        
        myDataTyped.data = 666;
        
        K Offline
        K Offline
        Kofr
        wrote on last edited by
        #3

        @Wieland said in Template dependant argument:

        decltype(myData)

        what does decltype(myData) returns so we can access its type member?

        ? 1 Reply Last reply
        0
        • K Kofr

          @Wieland said in Template dependant argument:

          decltype(myData)

          what does decltype(myData) returns so we can access its type member?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          @Kofr The type of myData is MyStruct<int>, thus decltype(myData) yields MyStruct<int>. See also: decltype specifier.

          1 Reply Last reply
          4
          • Chris KawaC Online
            Chris KawaC Online
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            Since both variables are supposed to be of the same type it can even be shortened as

            MyStruct<int> myData;
            decltype(myData) myDataTyped;
            
            1 Reply Last reply
            4

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved