f



C++14 std::size() non-member function

The C++14 working paper has voted in the following non-member functions, which
are missing from the C++11 standard:

std::cbegin()/std::cend(), std::rbegin()/std::rend(),
std::crbegin()/std::crend(). 

Seemingly another missing non-member function is std::size().

Such a function would be useful, especially when dealing with legacy functions that expect a pointer to an array and an array size. Many times the array size calculation of the array size is either hard coded or calculated using sizeof, both of which are error prone.

Consider the following possible implementation of the std::size() non-member
function.

// for raw arrays
template<typename Arr, size_t Size>
size_t inline size(Arr(&Array)[Size])
{
 return Size;
}

// for std containers implemented the size member function
template<typename Container>
auto inline size(Container& Cont) -> decltype(Cont.size())
{
 return Cont.size();
}

Using std::size() to calculate an array size would be as follows:

void some_legacy_func(wchar_t * p, size_t s);

wchar_t buf[15];
some_legacy_func(p, std::size(buf));
// the line above easier to get right than (sizeof(buf) / sizeof(buf[0]))

Is there any interest in such a function? If so, how could such a feature be proposed?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Gary
11/18/2013 5:31:53 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8510) is leader. Post Follow

0 Replies
254 Views

Similar Articles

[PageSpeed] 10

Reply:

Similar Artilces:

How to pass C++ member function to C function expecting function pointer?
I'm just starting to use C++, and don't want to have to rewrite all of the old C routines that I've been using for 20+ years. One of these is a general purpose function minimizer, which expects to be passed a pointer to the function to be minimized, double func(double p[]) How do I use this to minimize a function defined within a class in C+ +? To be specific, suppose I define the following class, which returns the sum of squared differences between an input vector and a (private) vector called trueParms: class max_func { public: max_func(vector <double> trueParms_) : trueParms(trueParms_) {}; double operator()(double p[]); private: vector <double> trueParms; }; double max_func::operator () (double p[]) { unsigned int i; double temp; double ss; for (i=0, ss=0.0; i< trueParms.size(); i++) { temp = p[i] - trueParms[i]; ss += temp * temp; } return ss; }; In the main code, if I have a vector v containing the values 1.0, 2.0, 3.0, then max_func ff(v); now defines a function ff(x) which returns (x[0] - 1)^2 + (x[1] - 2) ^2+ (x[2] - 3)^2 How do I create a function pointer, say ffpointer, in my C++ code that refers to ff, and which I can pass to my C minimizer? Thanks for any suggestions. Richard Stanton RHS wrote: > How do I create a function pointer, say ffpointer, in my C++ code that > refers to ff, and which I can pass to my C minimizer? ...

Passing a C++ object's member function to a C function expecing a function pointer!
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an external C struct (external to the C++ project/classes etc.) which is wants a function ptr assigned to one of it's members: struct blah { int (func_ptr*) (int a, int b, float c); }; etc... However, my code is in C++ and a wish to assign a non-static member function (I know I can do this) to this struct member! Can this be done!? Some how with pointers or references? What about the STL's mem_func stuff? Can anyone shed some light on this? Cheers Jim James, "James Vanns" <jimv@canterbury.a...

jython and C-c C-c
How do I get C-c C-c to work with jython? I have the jpython command set to jython, and I can start the interpreter with C-C ! and then use C-c C-c, but this is rather frustrating. If I try to use C-c C-c without first starting the interpreter in another window I get wrong type argument: sequencep, jpython Thanks, Dave Cook ...

Better C/C++ Than C/C++?
I am looking for a good systems programming language that can be used instead of C/C++. My qualifications for the language are: * mature compiler(s) that produce native-code binaries (for Windows) * open source - preferable, but not 100% necessary Thanks, Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) Ocaml, D, cyclone, Eiffel, Beta > * open source - preferable, but not 100% necessary what's wrong with using C? Friedrich > what's wrong with using C? Too low level, no built-in garbage collection. -- Kevin "Kevin Albrecht" <kevin@albrecht.net> writes: >> what's wrong with using C? > > Too low level, Well the Smalltalk people say, have a small language and a big library. I assume C's library status beyond that of Smalltalk. > no built-in garbage collection. Use the Boehm Weisser GC. Friedrich "Kevin Albrecht" <kevin@albrecht.net> wrote in message news:<2TiYb.237892$rj7.1641209@twister.tampabay.rr.com>... > I am looking for a good systems programming language > that can be used instead of C/C++. My qualifications > for the language are: > > * mature compiler(s) that produce native-code > binaries (for Windows) > ...

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

Calling C++ functions in C function through function pointers
Hi All!! I have a C++ program that uses callback funtions which are the private members of class. The code uses an API wrtiiten in C which supplies callback-setting functions that require pointers to these functions... The funtions wherein these API's callback-setting functions are called, are public members of the same class of which the callbacks are the private member. Now the API functions are generating compile errors since they cannot "understand" the C++ function pointers. Consider the sample code: class classname { public : /* Constructor for the class...

How to impliment searching the definition of a c/c++ function in a c/c++ source file
How to impliment searching the definition of a c/c++ function in a c/c+ + source file? are there any libs of opensource to use? I have no ideal about it, and any one who responses it will be appreciated. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] On Jun 18, 5:25 am, whiteid...@gmail.com wrote: > How to impliment searching the definition of a c/c++ function in a c/c+ > + source file? are there any libs of opensource to use? I have no > ideal about it, and any one who responses it will be a...

Which book is better for C++14 : Effective Modern C++ or C++14 FAQs ?
I am trying to understand C++14 from these books : Effective Modern C++ by Scott Meyers, and C++14 FAQs by Chandra Shekhar Kumar So far, I find the later book to a better one for C++14 whereas the first book is a generic one. But I do not see much of the reviews of the book C++14 FAQs on Amazon yet whereas the first book is highly rated, may be over rated ?? On Tuesday, April 14, 2015 at 3:57:31 PM UTC-5, lin.q...@gmail.com wrote: > I am trying to understand C++14 from these books : > > Effective Modern C++ by Scott Meyers, and > C++14 FAQs by Chandra Shekhar Kumar ...

BIND(C) INTERFACE for C function with internal C macro in the C API
Hi, I'm trying to figure out what my Fortran interface (with BIND(C)) for the code below should be. It gives the correct value in the C API for length of the string passed to C if I remove the BIND(C) statement. But when I add the BIND(C) statement it gives garbage for 'length', I tried to add an OPTIONAL INTEGER where STR_PLEN() is given but that just gives length=0. Thanks for the help. FORTRAN ============================ (using gfortran 4.8.2) MODULE test USE ISO_C_BINDING INTERFACE SUBROUTINE open_f(filename) BIND(C,NAME="open_f_") ...

c function in c++
error LNK2019: unresolved external symbol __imp__fwrite referenced in function "void __stdcall dg729(void)" (?dg729@@YGXXZ) error LNK2001: unresolved external symbol __imp__fwrite how to use .c function in c++ code mathudiv@gmail.com wrote: > error LNK2019: unresolved external symbol __imp__fwrite referenced in > function "void __stdcall dg729(void)" (?dg729@@YGXXZ) > > error LNK2001: unresolved external symbol __imp__fwrite > > how to use .c function in c++ code Make sure it is declared extern "C". -- Ian Collins. mathudiv@gmail.com wro...

C as a Subset of C++ (or C++ as a superset of C)
Isn't it a lame use of human time and effort to maintain completely separate C and C++ standards? As in the words of Betty White about Facebook: "It seems like an incredible waste of time". Why don't the two standards groups get together and agree on a common specification for the ground which both standards cover? There would still be two separate standards, but they'd both be exactly the same for the common ground. The common ground document could be referred to by both standards instead of being maintained by both groups in individual efforts resulting in...

slime C-c C-c annoyance
Does anyone have a good workaround for this problem other than: just be careful! When I've compiled a large package and slime has given me a *slime-compilation* buffer, I like to work through this buffer issue by issue, fixing the errors, warnings, etc. But If I ever press C-c C-c in the editor window while fixing an error, slime destroys the *(slime-compilation* buffer with the compilation output of C-c C-c. Jim Newton <jimka.issy@gmail.com> writes: > Does anyone have a good workaround for this problem other than: just be careful! > > When I've compiled a ...

C/C++ in matlab and matlab in C/C++ ?
Is it possible to read C/C++ code in matlab and the other way around too? Hi, as long as C or C++ are written in text files you can edit them in the matlab editor. To see the special character of C or C++ coding language, in the editor go to menus: file>preferences>Edito/Debugger>language and on popup "Language" select "C/C++". This does not allow you to run the C/C++ code, just to view it i nicer way... regards, chris saneman <asdfsdf@asd.com> wrote in message <frl846 $f8i$2@news.net.uni-c.dk>... > Is it possible to read C/C...

c------->c++ and c++------>c
We can write c program in turbo c++ editor.But reverse is not true why? i.e The program will not compile if u write c++ program in turbo c editor vim wrote: > We can write c program in turbo c++ editor.But reverse is not true why? > i.e The program will not compile if u write c++ program in turbo c > editor This isn't about C; this is about some editor. Why the fircone should /we/ know? Ask in a relevant group. [And /why/, in an editor specialised to some specific programming languages, would you expect it to compile a /different/ one? Would you expect turbo perl to compi...

C/C++ in MATALB or MATLAB in C/C++?
Hi I've been asking on the net how to use MEX properly so that I can use C/C++ in MATLAB, but information is not forthcoming. There seems to be a lot on using MATLAB in C/C++ but not the other way around. I understand Mathworks is trying very hard to sell the Matlab Compiler, but even so, why is it SO hard to get information about MEX? Or is it that I've missed something very fundamental, meaning that I'm struggling over something that is actually quite easy? Any thoughts and info would be great. Thanks BullDog BullDog wrote: > Hi > > I've been asking on the net ...

How to declare a C or C++ function in a Objective C class
Hi, I need to write few functions, which I need to pass to a method in Objective C class using the pointers to those functions. These functions use the instance variable of the class. Hence they must have access to the variables. I want to write functions in C or C++ because they are crucial to the efficiency of my code and I don't want to use selectors way of passing methods for the same reasons. Then where do I include the function declations and implementations in ..h and .mm files of the class. Ankush In article <1150755755.068251.207990@h76g2000cwa.googlegroups.com>, ankushiitk@gmail.com wrote: > Hi, > I need to write few functions, which I need to pass to a method in > Objective C class using the pointers to those functions. These > functions use the instance variable of the class. Hence they must have > access to the variables. > I want to write functions in C or C++ because they are crucial to the > efficiency of my code and I don't want to use selectors way of passing > methods for the same reasons. > Then where do I include the function declations and implementations in > .h and .mm files of the class. > Ankush Consider adding a userData parameter to your fast C++ functions. Define a struct that has a pointer to your Objective C object instance members. Add an initBridge Objective-C method that initializes the struct with pointers to objective-C members. Now, your fast function ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! Throne Software wrote: > Throne Software has opened up C, C++ and C# Forums at: > > http://www.thronesoftware.com/forum/ For all of your forums newsgroups already exist. > Join us! Why? Don't see any reason to support your marketing. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! ...

c,c++,obj-c
Of the 3 languages titled in this post is c++ older than obj-c ? Would someone experienced in C be able to dive right into C++ rather than obj-c ? Java is out for me now except applet programming I think I'm going to go with just C and C++. Bill Bill Cunningham wrote: > Of the 3 languages titled in this post is c++ older than obj-c ? Why does that matter? Do you want to use an old language? Try Fortran or LISP. > Would > someone experienced in C be able to dive right into C++ rather than > obj-c ? Not quite dive in, at least not into the OO parts of C++. ...

C, C++ and C# Forums
Throne Software has opened up C, C++ and C# Forums at: http://www.thronesoftware.com/forum/ Join us! In article <1120963701.862698.160260@g14g2000cwa.googlegroups.com>, Throne Software <mail@thronesoftware.com> writes >Throne Software has opened up C, C++ and C# Forums at: > >http://www.thronesoftware.com/forum/ > >Join us! Why? The beauty of news groups is that they come to me I don't have to go to them. Also all the people who have been around a while know what they are doing and are here. I noticed that there are some C and C++ Forums on another system...

C,, C++, Java, C#
I come from C,C++,Java and C#. What can SmallTalk do for me and for what cost? Regarding the Squeak thing, it's interesting though confusing. Is it an IDE? Why are the menus so wacky? "DM McGowan II" <nospam@nospam.net> wrote in message news:PLednX2vnLd11IncRVn-rA@comcast.com... > I come from C,C++,Java and C#. What can SmallTalk do for me ...? Ask not what Smalltalk can do for, but what can you do with Smalltalk. :-) In very few words, it significantly reduces time to market, while enhancig the development experience. This is because: 1. It...

Is this C or C++?
Simple question. If the task is (for example): "Write a C++ program which asks user his name (less than 20 chars) and prints it." Then, is this code a correct answer: char name[100]; cout<<"Your name?"<<endl; cin>>name; cout<<name<<endl; The point being, that the code uses C string "char name[]" and not C++ std::string. C is a subset of C++, so isnt it logically speaking a C++ program? On Tue, 19 Nov 2013 21:44:40 -0000, "crea" <no@com.notvalid> wrote: >Simple question. If the task is (...

c++ to c
Hello Everyone, I have a project where I need to write C++ code in C (due to OS issues). Can someone please show me exactly what I need to do to convert the code to C. Thanks Jami Here is the code I need to convert to C++: class CTestSuite : public CTestSuiteBase { public : CTestSuite(CTestBase *aTest); virtual ~CTestSuite(); virtual TInt InvokeTestL(int argc, char * argv[], TTestConfig * aConfig) ; IMPORT_C CTestSuite *GetTestSuite(); virtual int RunTestL(int aArgc, char *aArgv [], TTestConfig *aConfig); protected: CTestBase *iTest; } ; class CTestSuiteBase : p...

About C and C++
Hello, I will resume it like this: On the criterias of "complexity" and "difficulty", the C and C++ programming languages are more complex and difficult than Object Pascal for example, so since they are more complex and difficult they are likely to create something like a darwinian filter who don't let the weaker humans among us humans to cross or to climb the social ladder, this is why i think that C and C++ do participate to social darwinism, this is why i say that C and C++ are bad. Thank you, Amine Moulay Ramdane. On Sun, 08 Jun 2014 06:1...

Web resources about - C++14 std::size() non-member function - comp.lang.c++.moderated

Facebook Hopes to Increase Growth Through App Invites to Non-Members
In an effort to simultaneously increase the growth of Facebook and applications, Facebook’s growth team tonight announced the launch of an update ...

Amazon Prime Members May Spend More Than Double What Non-Members Do
... cloud storage for photos. A oft-quoted 2014 study from RBC Capital showed that Prime members spend nearly twice as much on Amazon as non-Prime ...

Facebook's messenger app opens up to non-members
Facebook's messenger app opens up to non-members Computer Business Review The Facebook CEO admitted to tech blogger Michael Arrington that ...

Non-members: Look at it this way - The Economist
Tusk gets into shape EVERY COUNTRY SEES “Europe” as a projection of its own hopes and fears.

Amazon Prime Members Spend Almost Twice as Much as Non-Members
The online retailer has taken steps to make sure Prime remains a good deal and the results of a new survey suggest why that's worth it to the ...

Supreme Court Says Unions Can't Bill Non-Members For Political Spending
The Supreme Court deals a blow to the SEIU's political efforts by banning the union from charging non-members special assessments for emergency ...

Facebook Building 'Shadow Profiles' of Non-Members, Experts Allege
Eight hundred million users are not enough. Facebook, the world's biggest social network, is now building profiles of non-users who haven't even ...

UN votes to recognize Palestine as non-member state
Sort of. This has more to do with Palestinian infighting than it does with Israel, and it’s highly likely to backfire. Which means it’s no different ...

National Journal Bans Reader Comments From Non-Members
... the Journal would "rather put our resources" into high-quality journalism. For now, the website will only eliminate commenting from non-members. ...

KC police union drops efforts to collect fees from non-members
April 1 was the deadline for non-members to pay a share of the union&#x2019;s legal fees or risk being disciplined. Union officials said the ...

Resources last updated: 2/22/2016 1:49:43 PM