[C++] Error C2075?

Pages: 12
Are you sure?
1
2
3
4
5
6
7
8
int main(){
	int array[]={0,1,2,3,4,5,6,7,8,9};
	std::cout
		<<sizeof(array)<<" / "<<sizeof(typeid(array))<<" = "<<sizeof(array)/sizeof(typeid(array))<<std::endl
		<<typeid(typeid(array)).name()<<std::endl
		<<typeid(array).name()<<std::endl;
	return 0;
}
You're right, that doesn't work. I have a typo in my code, inconsistent with what I've said before. (I've fixed it in my other posts)
Actual code (Plus your extra stuff)
1
2
3
cout<<sizeof(array)<<" / "<<sizeof(typeid(array).name())<<" = "<<sizeof(array)/sizeof(typeid(array).name())<<endl
<<typeid(typeid(array)).name()<<endl
<typeid(array).name()<<endl;


Output:

40 / 4 = 10
class type_info
int [10]


Question: Why don't you use using namespace std; rather then having a ton of std::'s in your code?
Last edited on
That's a coincidence.
1
2
3
4
5
int main(){
	char array[]={0,1,2,3,4,5,6,7,8,9};
	std::cout <<sizeof(array)<<" / "<<sizeof(typeid(array).name())<<" = "<<sizeof(array)/sizeof(typeid(array).name())<<std::endl;
	return 0;
}

As you can see, your code breaks if sizeof(*array)!=sizeof(const char *). Like I said before, it almost never works.

Question: Why don't you use using namespace std; rather then having a ton of std::'s in your code?
Answer: Because I a) know about namespace collisions and b) utterly despise using namespace. b) is a consequence of a), by the way.
Last edited on
You showed before that pointers don't work. That's why I said it works with all arrays. (array's != pointer's)
Did I use a pointer, there? array is clearly an array. Maybe the fact that array[0]==*array confused you.
Yes, it did.

But you're still (for some reason) using a pointer to check the size, right? if(sizeof(*array)!=sizeof(const char *)
I don't think you understand. *array and array[0] mean the exact same thing in every situation. If you can use one, you can use the other. Read my previous statement as
your code breaks if sizeof(array[0])!=sizeof(const char *)
if you prefer. It doesn't change the fact that the code depends on the size of const char * being the same as the size of an element in the array. Your previous code works merely by coincidence. Namely, the coincidence that sizeof(int)==sizeof(const char *) in your implementation, which is certainly not guaranteed, and even if it was guaranteed, there are many other types that are larger than const char *.

This. Simply. Does not. Work.
sizeof(typeid(T).name())!=sizeof(T)
Last edited on
I did not know that those two were the same.

But who is to guarantee that the type is a const char *? How that statement is written, it's not necessarily a const char *, but it is rather whatever type the element is.

Thus, it isn't
sizeof(int)==sizeof(const char *)
but rather
sizeof(int)==sizeof(int)


When you do sizeof(typeid(element)) typeid is changed to whatever type element is.

I fail to see why you are even putting const char * as unless element is declared such as:
const char *element then that wouldn't be what is put it.
First, typeid(T) is a completely different type than T. I verified this here: http://www.cplusplus.com/forum/general/21665/page2.html#msg113329
There's many ways to prove that typeid(T) and T are entirely different things. Here's two:
1
2
3
4
5
6
7
8
class A{ int a[100]; };
std::cout
    <<sizeof(A)<<std::endl
    <<sizeof(typeid(A))<<std::endl;

std::cout
    <<typeid(int).name()<<std::endl
    <<typeid(typeid(int).name())<<std::endl;
I think it's an instance std::type_info, though I'm not sure.

Second, const char * is the return type of std::type_info::name(). That's what allows you to print it. I verified that here: http://www.cplusplus.com/forum/general/21665/#msg113293
And it's also stated here (line 7 of the first code snippet): http://www.cplusplus.com/reference/std/typeinfo/type_info/
1
2
3
4
5
std::cout
    <<typeid(typeid(int).name()).name()<<std::endl
    <<typeid(const char *).name()<<std::endl;
const char *s=typeid(int).name(); //if the above wasn't true, this assignment should produce a compiler error.
std::cout <<typeid(s).name()<<std::endl;
I see what you say. But I don't see why you would ever do that.

The reason I use that method is a quick simple way to find the size of an array (excluding char as I would use strlen() for that) if you need to know.

Don't bother trying to explain further, I'm obviously not getting why you're saying what you are, so I'll figure this one out with experence. Thanks for trying though!
I use that method is a quick simple way to find the size of an array
Do what you want. All I can tell you is this: it doesn't work, and it'll come back to bite you in the ass if you get used to using it.
At no point should you ever need to "find" the size of an array. It's supposed to be known at all times.
meesa: I think you are confusing "typeid" with "typeof". typeof is not standard (yet) - only a gcc extension. It yield in the type of the parameter. But then again, sizeof(typeof(T)) is always the same as sizeof(T), so no need for typeof here either..

typeid is for returning meta information about objects. It returns an instance of type "type_info" which is a class and has a member function called "name()" and which in turn returns a "const char*". I thought before that it also could return "const wchar_t*" but I was wrong in that.

Hey, you could try this out. Get the typeid of a typid! ;-)
 
cout << typeid ( typeid(int) ).name() << endl;

My compiler prints "class type_info".


At no point should you ever need to "find" the size of an array. It's supposed to be known at all times.
I second that. If you use C-style arrays, there should be no uncertainity about the size of the array. If you still can't live without, at least use: sizeof(array) / sizeof(*array) or sizeof(array) / sizeof(array[0]) which is the same (as helios already pointed out).

There is a library "boost::array" which has practically no overhead over an C-style array, but which provides better means to obtain the size of an array. You could also just use std::vector and have "very little" overhead.


Ciao, Imi.
Topic archived. No new replies allowed.
Pages: 12