Can not get size of static array defined in class template - by sbauer23

Status : 

  Deferred<br /><br />
		The product team has reviewed this issue and has deferred it for consideration at a later time.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


4
0
Sign in
to vote
ID 759407 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 8/24/2012 7:48:32 AM
Access Restriction Public

Description

Just curious why the following does not work under Visual Studio (it does under gcc).

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>

// ***********************************************************
/** Macro to count the number of items in an array */
#define m_COUNT1(x)  (sizeof(x)/sizeof(x[0]))

// ***********************************************************

template<typename T, std::size_t size>
std::size_t GetArrLength(T(&)[size]) { return size; }

#define m_COUNT2(pArray) GetArrLength(pArray)

// ***********************************************************

template <typename T>
class TempClass
{
public:
    static int tempclass_array[];
    static int tempclass_array_count1_size;
    static int tempclass_array_count2_size;
};

template <typename T>
int
TempClass<T>::tempclass_array[] = 
{
    1,
    2,
    3,
    4,
};

template <typename T>
int
TempClass<T>::tempclass_array_count1_size = m_COUNT1(TempClass<T>::tempclass_array);

template <typename T>
int
TempClass<T>::tempclass_array_count2_size = m_COUNT2(TempClass<T>::tempclass_array);

class ImplClass : public TempClass<ImplClass>
{
public:
    static int implclass_array[];
    static int implclass_array_count1_size;
    static int implclass_array_count2_size;
};

int
ImplClass::implclass_array[] = 
{
    1,
    2,
    3,
    4,
};

int
ImplClass::implclass_array_count1_size = m_COUNT1(ImplClass::implclass_array);

int
ImplClass::implclass_array_count2_size = m_COUNT2(ImplClass::implclass_array);

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout << "size of tempclass_array is " << ImplClass::tempclass_array_count1_size << std::endl;
    std::cout << "size of tempclass_array is " << ImplClass::tempclass_array_count2_size << std::endl;
    std::cout << "size of implclass_array is " << ImplClass::implclass_array_count1_size << std::endl;
    std::cout << "size of implclass_array is " << ImplClass::implclass_array_count2_size << std::endl;
	return 0;
}



Sign in to post a comment.
Posted by ildjarn on 6/3/2015 at 7:25 PM
I can confirm that this problem is still present in compiler version 19.00.22929.
Posted by ildjarn on 5/14/2015 at 3:01 AM
I can confirm that this problem is still present in compiler version 19.00.22816.
Posted by brettbjackson on 7/3/2014 at 2:46 PM
Any ETA on this will be fixed??
Posted by Microsoft on 8/26/2012 at 7:15 PM
Thanks for your feedback.

We are rerouting this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 8/24/2012 at 7:53 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)