Can one access the template parameter outside of a template without a typedef?

后端 未结 2 1560
日久生厌
日久生厌 2020-12-16 22:27

A simple example:

template // this template parameter should be usable outside!
struct Small {
   typedef _X X; // this is tedious!
   X f         


        
2条回答
  •  遥遥无期
    2020-12-16 22:36

    Depending on what you're doing, template template parameters might be a better option:

    // "typename X" is a template type parameter. It accepts a type.
    // "template  class SomeSmall" is a template template parameter.
    // It accepts a template that accepts a single type parameter.
    template class SomeSmall> 
    struct Big {
       SomeSmall bar; // We pass X to the SomeSmall template.
       X toe; // X is available to this template.
    }; 
    
    // Usage example:
    
    template
    struct Small { 
       X foo; 
    };
    
    struct MyType {};
    
    // The foo member in Small will be of type MyType.
    Big big;
    

提交回复
热议问题