Can I access the struct / class element from a template?

What I want to do is create a function that accepts any class or structure (via templates), but also has a function assuming that there always exists a specific member in the class or pass-in structure.

In probably the wrong code, it looks something like this:

template <class inputType>
int doSomething(inputType voxel)
{
    return voxel.density;
}

I want him to suggest that the member densitywill always be there and that he will always be int(or something else). I can do it? And if so, what happens if it densitydoes not exist? Would it just throw a compiler error?

+5
source share
2 answers

, ++ (, Java), , .

++ , , . , , doSomething , .

+4

, , , , . ​​ , , , , .

, LiveWorkSpace C/++ , . , . .

+1

All Articles