Given a class:
template <typename T>
class foo
{
public:
struct bar
{
bar(T value) { m_foo = value; }
T m_foo;
...
}
...
}
and an instance:
foo<int> myfoo;
is it possible to create an instance of bar
without having to specify myfoo
's types again like this:
myfoo::bar mybar;
// foo<int>::bar mybar;
when I try this I get an error-pair (VS 2015) :
'myfoo': is not a class or namespace name
'bar': identifier not found
UPDATE here is a more detailed example of what I want to achieve:
template <typename T, typename T2, typename T3>
class MyClass
{
public:
struct MyClassCreateParameters
{
MyClassCreateParameters( T t, T2 t2, T3 t3 )
:
T1Param( t ), T2Param( t2 ), T3Param( t3 ) { }
T T1Param;
T2 T2Param;
T3 T3Param;
};
MyClass( const MyClassCreateParameters& params )
:
m_t1( params.T1Param ),
m_t2( params.T2Param ),
m_t3( params.T3Param ) { }
private:
const T m_t1;
const T2 m_t2;
const T3 m_t3;
};
int main()
{
MyClass< int, char, char* >* myclass;
myclass = new MyClass< int, char, char* >(
myclass::MyClassCreateParameters( 1, 'A', "abc" ) );
return 0;
}
使用C ++ 11,您应该能够:
decltype(myfoo)::bar mybar;
On the face of it, I would use a typedef:
template <typename T1, typename T2, typename T3>
class MyClassTemplate
{
public:
struct Params { /* ... */ };
MyClassTemplate(Params);
// ...
};
using MyClass = MyClassTemplate<int, char, char*>;
MyClass x(MyClass::Params(a, b, c));
However, you could also endow your class template with a forwarding constructor template:
template <typename T1, typename T2, typename T3>
class MyClassTemplate
{
private:
struct Params { /* ... */ };
MyClassTemplate(Params);
public:
template <typename ...Args>
MyClassTemplate(Args ...args) : MyClassTemplate(Params(args...)) {}
// ...
};
MyClassTemplate<int, char, char*> x(a, b, c);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.