¿Inicializando el miembro estático de la clase con plantilla anidada durante la especialización de plantilla anidada?
Frecuentes
Visto 1,198 veces
1
Aquí está mi problema:
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
private:
static int count;
};
static int code;
void print() const
{
std::cout << "generic";
}
};
template<>
template<>
class Outer<bool>::Inner<bool>
{
static int count;
};
template<>
template<>
int Outer<bool>::Inner<bool>::count = 4; // ERROR
How do I initialize it properly?
2 Respuestas
6
Fully-specialized templates are in fact no longer templates, so your definition should simply be:
int Outer<bool>::Inner<bool>::count = 4;
In full, with all definitions in place, your code should look like:
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
private:
static int count;
};
static int code;
void print() const
{
std::cout << "generic";
}
};
template<typename T>
int Outer<T>::code = 0;
template<typename T>
template<typename U>
int Outer<T>::Inner<U>::count = 0;
template<>
template<>
class Outer<bool>::Inner<bool>
{
static int count;
};
int Outer<bool>::Inner<bool>::count = 4;
contestado el 03 de mayo de 12 a las 19:05
0
To get one static member per Inner template class instance you can as an alternative use a separate template class.
template<typename T>
class Outer
{
public:
template<typename U>
class Inner
{
};
};
template <typename T>
struct InnerStatic
{
static int count;
};
template <typename T>
int InnerStatic<T>::count = 4;
int main(int argc, char** argv)
{
std::cout << InnerStatic<Outer<int>::Inner<int>>::count << std::endl;
return 0;
}
Respondido el 23 de junio de 18 a las 12:06
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ templates static or haz tu propia pregunta.
Hi ildjarn, Pasted your code into visual studio and I'm getting following error: error C2906: 'int Outer<T>::Inner<bool>::count' : explicit specialization requires 'template <>' Can you exlplain that? - codigokiddy
@codekiddy : I tested this exact code (plus
#include <iostream>
) in VC++ 2010 SP1 without errors. It also works with GCC 4.3.4 y GCC 4.5.1. What version of VC++ are you using? (And the error message definitely implies that you changed my code somehow, as it's complaining aboutOuter<T>::Inner<bool>::count
más bien queOuter<bool>::Inner<bool>::count
.) - ildjarnUsing Visual studio 11 Beta platform toolseet V110 :) - codigokiddy
@codekiddy : I don't have that installed, sorry. Either way, the code shown here is correct; I suspect you did modify it somehow. ;-] - ildjarn
No I didn't modify the code, It may be a bug in VS Beta 11, I'm going to download VS 2010 now and test it cos I wanna learn that. thank you for your help! - codigokiddy