Link to home
Start Free TrialLog in
Avatar of ugeb
ugebFlag for United States of America

asked on

Template syntax for variable length arrays

I'm playing around with some C++ and I seem to have some difficulty with getting the correct syntax for using templates to create a variable sized array.  For example, we know that this code is invalid:

for (int i=1; i< 5;i++) 
{
   int a[i] = {0}; // invalid
   int * a = new int[i];  //valid
}

Open in new window

How would I define a template to instantiate an array of length N and type T? I was wanting something like this declaration, if fixedArr is the template:
fixedArr<double,8> fa;

which would declare a double type array of length 8.  What is the syntax to declare the template and instantiate it to declare the array?

(Visual C++, C++11, no .Net)
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ugeb

ASKER

Thank you for that.

If this is just an exercise in remembering how to use template, then you can do this:
Bingo!  I'm familiar with vectors, but I'm remembering the caveats of template syntax. I'm wondering what % of the US is using C++11 now, as it is definitely an improvement.

I see that the arrays you declare must be prefixed by the struct.  Is there a way to eliminate the struct or perhaps use a typedef so I don't need to type the struct prefix?
Avatar of phoffric
phoffric

In C, there was typedef which at least informed the programmer of the intent of the type. But the typedef in C (and C++) did not provide type safety.

In C++, the struct is a class where, by default, its members (i.e., data and functions) are public. As I said earlier, "You can then change the struct to a class and make the myArray member private."

In C++, I think you should avoid the typedef keyword. But I am a bit biased because my previous company required that typedef's not be used unless the type was about 200+ chars long. (And this happened a bit when using boost; in fact, some of the typedef's were close to 400 chars long.)

If you use the class instead of the struct, and add the extra functions/operators (and include, say, an int numElements, for convenience), but by the time you are done, you may have just reinvented the wheel of std::array.
>> I'm wondering what % of the US is using C++11 now, as it is definitely an improvement.

I don't know the relative numbers of the various languages. A couple of years ago I looked it up and saw that C++ was not losing out too much to Java/C#/Python. And for the past 16 years, I have had no problems getting positions that required C++ as a required skill. If I start to find out that this skill is no longer needed someday, well, maybe I'll learn a new language. (Hmm, I just signed up for Coursera Python free courses because my client's customer requested that we switch from C++ to Python 2.7 - no idea why, but it is a good scripting language, so why not learn it.)

I just looked your question up within a one year window, and this was at the top of the search.
http://www.infoworld.com/article/2947536/application-development/javascript-rules-the-school-but-c-climbs-in-popularity.html

And then I tried to find %'s. And this is what I came up with:
http://www.tiobe.com/tiobe_index?page=index

Now, since this is on the WWW, we know it must be true. :S
More off the topic stuff in case you are seriously thinking of relearning or becoming expert at C++.

Here's an article from John Sonmez  - best selling author of the book "Soft Skills: The Software Developer's Life Manual." (No, I haven't read it and haven't heard of him.)
https://simpleprogrammer.com/2012/12/01/why-c-is-not-back/

But John must be someone of good standing or the great Herb Sutter would not have written a critique:
https://herbsutter.com/2012/12/03/perspective-why-c-is-not-back/

Now, here's personal complaint from a former manager of elite C++/Boost developers (one was even on the boost committee for Boost Spirit). He told me that he was having trouble getting candidates to pass their interview (which didn't even touch upon boost). He said that the tech schools (nearby in Virginia, USA) were teaching Java, of all things! Useless for his group! (Well if you believe the % usage chart link I posted, you see that Java has a huge market share. He had difficulty in keeping up with the project goals due to lack of competent C++ developers. I think they shot themselves in the foot by using very complex C++/Boost code which made the C++ code look alien to even decent C++ programmers.
Avatar of ugeb

ASKER

Very interesting.  I'm relearning C++ now, though I prefer Python (and in some cases, Java).  In general I understand where John Sonmez is coming from.  For new developers who have the luxury of choosing what language they want to learn, Python, Java, etc. make a lot of sense.  The higher level of abstraction is a welcome change from the complex and often confusing syntax of C++.

However, John did miss at least one valid use case.  Many companies have code that was developed in C++ and there is no chance they will rewrite it.  Cobol stuck around for how long and I hear there are still systems that run it.  Who even makes Cobol compilers anymore?  Not sure about Fortran.  But many companies hire developers who can jump in on the code base currently in operation, and there is a ton of C++ out there.  If someone is beginning a new development project, they should seriously consider using Python or Java.

Python is interpreted and Java runs in semi-compiled byte code.  I come from a finance background where C++ is heavily utilized because of it's speed -- but definitely not speed of development.  Still, banks and other financial companies often use C++.  So, for that reason it makes sense to revisit it.  Still prefer the power and elegance of Python (love those list comprehensions!), but I need to know several languages nowadays, and it's been awhile from my C/C++ days.
In my current contract, I had to convert Fortran 90 to C++ using valarray. F90 was faster. Even when I used Intel Performance Primitives to make use of SSE3 SIMD, F90 still came out ahead. With more research, I might have been able to try to come closer to F90, but that task ended. Then again, the F90 code could have been tweaked  to use the keyword, intent, to avoid unnecessary copying out of a function huge (GB) matrices. F90 can be better optimized than C++ or C, because of those pesty pointers in C/C++.

Long live COBOL. I picked the manual up and got through page one and put it back in the companies shelf.
Avatar of ugeb

ASKER

Great.  Thank you for the help and the interesting conversation!
You're welcome. The off-topic conversations were interesting to me as well. Always nice to look up trends. Good to see that C++ isn't heading downhill too much. But when C++ is designed to be overly clever to show off how much we know, then the code can only be readily understood by elite developers, and the project has a potential problem as more developers are needed; it is hard to find or very expensive to find these elite developers.