instantiated from here error with a predicate template function - c++

I tried researching this question already, but it seems as though every question dealing with my error has to do with classes and their default constructors whereas my code contains no classes. It's a simple size() function that returns the number of elements in a given array. (I'm aware there is a built in size() function, that's not the point).
I at first thought that it was upset with my naming my function size() when a size() function already existed so I changed mine to sise() but I still recieve the same error and have no idea how to solve it.
Code:
template<class T > int sise(T array[], int count){
if(array[count+1]== NULL){
return count+1;
}
else{
return sise(array,count+1);
}
}
template <class T> int sise(T array[]){
return sise(array , 0);
}
int main(){
int array[] = {1 , 7 , 5, 4, 6 ,2 , 3};
int len = sise<int>(array);
std::cout << len << std::endl;
//print<int>(array);
// shakersort<int>(array);
// print<int>(array);
return 0;
}
Don't worry about the commented out function calls in main(), as each respective's calls functions have been commented out but the same error of
Shakersort.cpp: In function ‘int sise(T*, int) [with T = int]’:
Shakersort.cpp:60:24: instantiated from ‘int sise(T*) [with T = int]’
Shakersort.cpp:77:28: instantiated from here
Shakersort.cpp:51:3: warning: NULL used in arithmetic [-Wpointer-arith]
is given. What is going on?

The compiler is warning about NULL being compared to an int from the looks of it. Not strictly an error (nor is it made an error) but a likely indication of false assumptions being made. One such assumption seems to be that arrays are magically null-terminated: they are not. That is something very specific to string literals and even then the null-terminator is not NULL but rather '\0'. You'll either need to pass the size of the array along or you'll need to deduce it using a template, e.g.:
template <typename T, int Size>
int sise(T (&array)[Size], int count) {
...
}

Related

C++ std::copy error: request for member ‘end’ in ‘__cont’, which is of non-class type ‘int* const’ [duplicate]

Is it possible to determine the size of an array if it was passed to another function (size isn't passed)? The array is initialized like int array[] = { XXX } ..
I understand that it's not possible to do sizeof since it will return the size of the pointer .. Reason I ask is because I need to run a for loop inside the other function where the array is passed. I tried something like:
for( int i = 0; array[i] != NULL; i++) {
........
}
But I noticed that at the near end of the array, array[i] sometimes contain garbage values like 758433 which is not a value specified in the initialization of the array..
The other answers overlook one feature of c++. You can pass arrays by reference, and use templates:
template <typename T, int N>
void func(T (&a) [N]) {
for (int i = 0; i < N; ++i) a[i] = T(); // reset all elements
}
then you can do this:
int x[10];
func(x);
but note, this only works for arrays, not pointers.
However, as other answers have noted, using std::vector is a better choice.
If it's within your control, use a STL container such as a vector or deque instead of an array.
Nope, it's not possible.
One workaround: place a special value at the last value of the array so you can recognize it.
One obvious solution is to use STL. If it's not a possibility, it's better to pass array length explicitly.
I'm skeptical about use the sentinel value trick, for this particular case. It works
better with arrays of pointers, because NULL is a good value for a sentinel. With
array of integers, it's not that easy - you need to have
a "magic" sentinel value, which is
not good.
Side note: If your array is defined and initalized as
int array[] = { X, Y, Z };
in the same scope as your loop, then
sizeof(array) will return it's real size in bytes, not the size of the pointer. You can get the array length as
sizeof(array) / sizeof(array[0])
However, in general case, if you get array as a pointer, you can't use this trick.
You could add a terminator to your int array then step through the array manually to discover the size within the method.
#include<iostream>
using namespace std;
int howBigIsBareArray(int arr[]){
int counter = 0;
while (arr[counter] != NULL){
counter++;
}
return counter;
}
int main(){
int a1[6] = {1,2,3,4,5,'\0'};
cout << "SizeOfMyArray: " << howBigIsBareArray(a1);
}
This program prints:
SizeOfMyArray: 5
This is an O(n) time complexity operation which is bad. You should never be stepping through an array just to discover its size.
If you can't pass the size, you do need a distinguishable sentinel value at the end (and you need to put it there yourself -- as you've found, you can't trust C++ to do it automagically for you!). There's no way to just have the called function magically divine the size, if that's not passed in and there is no explicit, reliable sentinel in use.
Can you try appending a null character \0 to the array and then send it? That way, you can just check for \0 in the loop.
Actually Chucks listing of
for( int i = 0; array[i] != NULL; i++) {
........
}
A sizeof before each call is wasteful and is needed to know what you get.
Works great if you put a NULL at the end of the arrays.
Why?? With embedded designs passing a sizeof in each routine makes each call very large compared to a NULL with each array. I have a 2K PIC16F684 chip and it takes upto 10 percent of the chip with 12 calls using a passed sizeof along with the array. With just the array and Chucks code with NULLS om each array... I get 4 percent needed.
A true case in point.. thanks chuck good call.
I originally had this as an answer to this other question: When a function has a specific-size array parameter, why is it replaced with a pointer?, but just moved it here instead since it more-directly answers this question.
Building off of #Richard Corden's answer and #sbi's answer, here's a larger example demonstrating the principles of:
Enforcing a given function parameter input array size using a reference to an array of a given size, like this:
void foo2(uint8_t (&array)[100])
{
printf("sizeof(array) = %lu\n", sizeof(array));
}
and:
Allowing a function parameter input array of any size, by using a function template with a reference to an input array of a given template parameter size N, like this:
template<size_t N>
void foo3(uint8_t (&array)[N])
{
printf("sizeof(array) = %lu\n", sizeof(array));
}
Looking at the full example below:
Notice how this function prototype doesn't know the array size at all! (the 100 here is simply a visual hint/reminder to the human user, but has no bearing or influence on the compiler whatsoever!):
void foo(uint8_t array[100]) {}
...this function prototype allows only input arrays of a fixed size of 100:
void foo2(uint8_t (&array)[100]) {}
...and this function template prototype allows arrays of ANY input size AND knows their size statically at compile-time (as that is how templates work):
template<size_t N>
void foo3(uint8_t (&array)[N]) {}
Here's the full example:
You can run it yourself here: https://onlinegdb.com/rkyL_tcBv.
#include <cstdint>
#include <cstdio>
void foo(uint8_t array[100])
{
// is ALWAYS sizeof(uint8_t*), which is 8!
printf("sizeof(array) = %lu\n", sizeof(array));
}
void foo2(uint8_t (&array)[100])
{
printf("sizeof(array) = %lu\n", sizeof(array));
}
template<size_t N>
void foo3(uint8_t (&array)[N])
{
printf("sizeof(array) = %lu\n", sizeof(array));
}
int main()
{
printf("Hello World\n");
printf("\n");
uint8_t a1[10];
uint8_t a2[11];
uint8_t a3[12];
// Is `sizeof(array) = 8` for all of these!
foo(a1);
foo(a2);
foo(a3);
printf("\n");
// Fails to compile for these 3! Sample error:
// > main.cpp:49:12: error: invalid initialization of reference of type ‘uint8_t (&)[100]
// > {aka unsigned char (&)[100]}’ from expression of type ‘uint8_t [10] {aka unsigned char [10]}’
// > foo2(a1);
// > ^
// foo2(a1);
// foo2(a2);
// foo2(a3);
// ------------------
// Works just fine for this one since the array `a4` has the right length!
// Is `sizeof(array) = 100`
uint8_t a4[100];
foo2(a4);
printf("\n");
foo3(a1);
foo3(a2);
foo3(a3);
foo3(a4);
printf("\n");
return 0;
}
Sample output:
(compiler warnings, referring to the sizeof call inside foo()):
main.cpp:26:49: warning: ‘sizeof’ on array function parameter ‘array’ will return size of ‘uint8_t* {aka unsigned char*}’ [-Wsizeof-array-argument]
main.cpp:23:27: note: declared here
(stdout "standard output"):
Hello World
sizeof(array) = 8
sizeof(array) = 8
sizeof(array) = 8
sizeof(array) = 100
sizeof(array) = 10
sizeof(array) = 11
sizeof(array) = 12
sizeof(array) = 100
Shouldn't this work? for things like Arduino(AVR) c++ at least.
//rename func foo to foo_ then
#define foo(A) foo_(A, sizeof(A))
void foo_(char a[],int array_size){
...
}

'Constant Expression Required' Error while keeping formal argument as a constant

This is a C++ programming code to display the values of array1 and array2 but I am getting a compile time error as 'Constant Expression Required'. Please Help
void display(const int const1 = 5)
{
const int const2 = 5;
int array1[const1];
int array2[const2];
for(int i = 1 ; i < 5 ; i++)
{
array1[i] = i;
array2[i] = i * 10;
std::cout << array1[i] << std::endl;
}
}
void main()
{
display(5);
}
In C++, const is not always constexpr. Back in the days, constexpr didn't exist, so the only way of having a compile time constant was to either use const with a literal, or to use enum, because both of these are easy for the compiler to check the value.
However, in C++11, we added constexpr, which guaranties that a constexpr variable has a value available at compile-time, and state that constexpr function can be evaluated aat compile time if all arguments are constexpr too.
In your code, you can write your variable const2 like this:
void display(const int const1=5)
{
constexpr int const2 = 5;
// ...
}
Now your code is much more expressive about what you are doing. instead of relying that the const may be available at compile time, you say "this variable has a value known at compile time, here's the value".
However, if you try to change const1, you'll get an error. Parameters, even with default value always as a value known at runtime. If the value is only known at runtime, you can't use it in template parameters or array size.
If you want your function to be able to receive the value const1 as a constant expression from where you can receive it as a template parameter, since template parameters are always known at compile time.
template<int const1 = 5>
void display()
{
constexpr int const2 = 5;
int array1[const1];
int array2[const2];
}
You will have to call your function like that:
// const1 is 5
display();
// const1 is 10
display<10>();
If you want to know more about templates, go check Function templates, or this tutorial

Array and sizeof troubles compiling errors C++

I'm writing an array-based code that is throwing me some confusing errors after failing to compile. I have searched the internet for sample code that I understand more or less but it is helpful for me to identify errors in my own code / thought process.
The task at hand is to create a function that accepts an array of an unknown amount of integers and sums the even numbers in the array. I am told the last entry of the array is -1, but I don't think this information is useful.
Here is my code:
#include <iostream>
using namespace std;
int sumEven(int myArray[])
{
int len = (sizeof(myArray) / sizeof(myArray[0]));
int i = 0;
int count = 0;
while (i < len)
{
if (myArray[i] % 2 == 0)
{
count = count + myArray[i];
}
i++;
}
return count;
}
I attempt to define len as the number of array elements. I think this didn't work since one error refers to this line:
prog.cpp:13:18: error: sizeof on array function parameter will return size of 'int *' instead of 'int []' [-Werror,-Wsizeof-array-argument]
int len = (sizeof(myArray) / sizeof(myArray[0]));
^
prog.cpp:11:17: note: declared here
int sumEven(int myArray[])
^
1 error generated.
I have experience with Matlab, Mathematica, and Python, and so my C++ formatting may be strange. Thanks for taking a look.
The problem is that when passed as arguments to a function, arrays decays to pointers to the first element, so the function
int sumEven(int myArray[]) { ... }
is actually equal to
int sumEven(int *myArray) { ... }
And taking the size of a pointer returns the size of the pointer and not what it points to.
If you need to know the size in the function, you should pass the number of elements as an argument:
int sumEven(int *myArray, size_t len) { ... }
Arrays decay to pointers when you pass them to a function. That means that the information regarding the size of the array is lost.
This means that (sizeof(myArray) / sizeof(myArray[0])) will not do what you want in this context, because here myArray is a pointer.
The canonical solution is to add another parameter representing the array size, and use that instead. This is the approach used in C.
In C++, however, you should probably be using std:: vector, unless you have a specific reason to stick with arrays, which are notoriously error-prone.

recursive variadic template function call "loses" pointer on second argument type

I've got a function (GetArgs) using a variadic template that sorts through it's arguments (each a pointer), sending each to another function (GetArg) that has been overloaded for each type (at the moment int & float). The overloaded funtions for each type then set the value at the pointer.
It compiles fine, and when I call the function with however many arguments of a single type it runs fine. However if I use the two types (float & int), it will run fine until the first occurrence of the second type, crashing because (from what I can tell) the pointer address is null (0x0 in debugger).
Here are my function definitions(declared in namespace included in main):
namespace.h
int GetArg(int iStackPos,int *i);
int GetArg(int iStackPos,float *f);
template<typename tFirst> int GetArgs(tFirst first)
{
GetArg(-1,first);
}
template<typename tFirst, typename... tRest> int GetArgs(tFirst first, tRest... rest)
{
int iStackPos = ((sizeof...(rest) + 1) * -1);
GetArg(iStackPos,first);
GetArgs((rest)...);
return 0;
}
namespace.cpp
int GetArg(int iStackPos,int *i)
{
*i = 1;
}
int GetArg(int iStackPos,float *f)
{
*f = 2.5;
}
call to get args in main:
would run fine:
int *i1;
float *f1;
namespace::GetArgs(f1,f1,f1,f1); //no use of int
would crash:
int *i1;
float *f1;
namespace::GetArgs(f1,i1,f1); //use of int
The same is true when int comes first and float after.
The null pointer happens in the recursive GetArgs() call, and the crash occurs because the overloaded int GetArg() tries to write to that.
The iStackPos is used as position on lua stack, as this is meant to be a wrapper to get arguments from lua. However I've replaced lua code with just assigning int 1, float 2.5, as the lua code wasn't the issue.
As others already suggested, you're passing uninitialized memory into your function. That produces undefined behavior.
Also, you're not returning anything in the GetArg functions and the first GetArgs. This can get you into trouble as well.
And your combining size_t and int when calculating iStackPos.

Get size of an array-pointer template parameter

I wondered if I could auto deduce the size of an array, which is passed as a template parameter, without (explicitly) passing its size.
The following code both compiles warning-less on g++ 4.8 and clang++ 3.3 (using -std=c++11 -Wall).
#include <iostream>
template<const int* arr>
struct array_container
{
static constexpr int val = arr[1];
array_container() {
std::cout << val << std::endl;
}
// static constexpr int arr_size = ??;
};
constexpr int one[] = { 1 };
constexpr int two[] = { 1, 2 };
int main()
{
// array_container<one> array_one;
array_container<two> array_two;
// (void) array_one;
(void) array_two;
return 0;
}
However, if I remove the two comment signs in main(), I get an out of bound error with both compilers.
Now, this is cool. Somehow the compiler knows the size of the array, though the type of const int* arr is a pointer. Is there any way to get the size of arr, e.g. to complete my comment in array_container?
Of course, you are not allowed to
Use any macros
Store the size in arr (e.g. passing an std::array as template parameter: constexpr std::array<int, 1> one = { 1 }, or using an end marker like '\0' in strings)
Use an additional template parameter for the size that can not be auto deduced (array_container<1, one> array_one).
Maybe std::extent template from <type_traits> header of C++11 standard library is what you want:
#include <iostream>
#include <type_traits>
constexpr int one[] = { 1 };
constexpr int two[] = { 1, 2 };
int main()
{
std::cout << std::extent<decltype(one)>::value << std::endl;
std::cout << std::extent<decltype(two)>::value << std::endl;
return 0;
}
Output:
1
2
template<size_t size>
constexpr size_t arraySize ( const int ( &arrayRef ) [size] ) {
return size;
}
int main(){
int A[1];
int B[2];
cout << arraySize(A) << arraySize(B);
return 0;
}
I believe something like this is what you're looking for, using array references. The syntax for declaring an array reference looks kind of like the syntax for a function pointer. This function template accepts an array reference named arrayRef, which prevents array-to-pointer decay so that compile-time info about array size is preserved. As you can see, the template argument is implicit to the compiler. Note that this can only work when the size can be deduced at compile time. Interestingly, this should still work without naming arrayRef at all. To make the above template more useful, you can add a template parameter to deduce the type of the array as well. I left it out for clarity.
Probably not, as SFINAE only happens in the immediate context, while that error comes from the requirement that UB in constexpr lead to a compile time error, which I think is not immediate. You could try a recursive SFINAE that stops on the UB, but even if it worked you would have to both check the standard and hope it does not change (as it is rather obscure and new).
The easy way is to ise s function to deduce the array size, have to explicitly pass it to the type, then store it in an auto. Probably not what you want.
There are proposals to allow type parameters to be deduced from value parameters, so you could wait for those instead.
Not a solid answer, more of an extended comment, so marked community wiki.
It is indeed possible. I found a solution using SFINAE. What it basically does is produce a substitution error if the index is out of bound (line 3 in this example):
template<class C>
static yes& sfinae(typename val_to_type<
decltype(*C::cont::data), *(C::cont::data + C::pos)>::type );
template<class C>
static no& sfinae(C );
The full source code is on github.
There are only two disadvantages:
You have to specify the type of the array (this can not be avoided)
It only works with g++ 4.8.1 and clang 3.3. g++ fails for empty strings (with a compiler bug). If someone can test for other compilers, that would be appreciated.