Why this code can't be compiled??

W

Wayne Shu

The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::cout, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pdarr, sizeof(darr) / sizeof(double));
std::cout << "after insort sort:\n";
display(std::cout, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Thanks.

Regards
Wayne Shu
 
E

Eric

Wayne said:
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::cout, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pdarr, sizeof(darr) / sizeof(double));
std::cout << "after insort sort:\n";
display(std::cout, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Thanks.

Regards
Wayne Shu


What error do you get when you try to compile?
 
T

Thomas Tutone

Wayne said:
Why this code can't be compiled??

Because it's not legal C++.
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];

What is "type"? It's not defined.
int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));


There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

std::cout << "after insort sort:\n";
display(std::cout, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pdarr, sizeof(darr) / sizeof(double));

Again, there's no way for the compiler to deduce the type of FUN here.
Change the above line to:

std::cout << "after insort sort:\n";
display(std::cout, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.

Best regards,

Tom
 
V

VJ

Thomas said:
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];


What is "type"? It's not defined.


Yes, instead of type should put T

int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));



There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int, std::less<int> >(piarr, sizeof(iarr) /
sizeof(int));


This gives next warning:

taking address of temporary

Anyone know a way to remove it?
 
J

Jim Langston

VJ said:
Thomas said:
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];


What is "type"? It's not defined.


Yes, instead of type should put T

int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));



There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

insert_sort<int, std::less<int> >(piarr, sizeof(iarr) /
sizeof(int));


This gives next warning:

taking address of temporary

Anyone know a way to remove it?


sizeof( iarr[0] ) maybe?
 
W

Wayne Shu

"Thomas Tutone дµÀ£º
"
Wayne said:
Why this code can't be compiled??

Because it's not legal C++.
The code is below:
#include <iostream>
#include <functional>

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
{
for( int j = 1; j < size; j++ ){
type key = arr[j];

What is "type"? It's not defined.
sorry, "type" should be "T", I forgot to modify it.
int i = j - 1;
while( i >= 0 && f(arr, key) ){
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0 };
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));


There's no way for the compiler to deduce the type of FUN here. Change
the above line to:

std::cout << "after insort sort:\n";
display(std::cout, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pdarr, sizeof(darr) / sizeof(double));

Again, there's no way for the compiler to deduce the type of FUN here.
Change the above line to:

std::cout << "after insort sort:\n";
display(std::cout, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;

return 0;
}

What's wrong??
Why the default arguement doesn't work??

Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.

If I want to the default argument work, how to change the code??
 
T

Thomas Tutone

Wayne said:
"Thomas Tutone дµÀ£º
Because it's not legal C++.
[snip]
Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.
If I want to the default argument work, how to change the code??

I have no idea whether your code is correct, but the way to make the
default argument work is to overload the template to have another
version of insert_sort that only takes two arguments. Try this:

#include <iostream>
#include <functional>

template <typename T, typename Fun>
void insert_sort(T *arr, size_t size, Fun f)
{
for( int j = 1; j < size; j++ ){
T key = arr[j];
int i = j - 1;
while( i >= 0 && f(arr, key) ) {
arr[i+1] = arr;
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
inline void insert_sort(T *arr, size_t size)
{
insert_sort(arr, size, std::less<T>());
}


template <typename T>
void display(std::eek:stream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr << ((i == size - 1) ? ">" : " ");
}
}

int main()
{
const int iarr[] = { 9, 8, 56, 784, 2, 7, 89, 4 };
const double darr[] = { 7.8, 89.2, 45.0, 44.0, 9.6, 78.4, 999.0
};
int *piarr = new int[sizeof(iarr) / sizeof(int)];
double *pdarr = new double[sizeof(darr) / sizeof(double)];

std::cout << "before sort:\n";
display(std::cout, iarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(piarr, iarr, sizeof(iarr));
insert_sort(piarr, sizeof(iarr) / sizeof(int));
std::cout << "after insort sort:\n";
display(std::cout, piarr, sizeof(iarr) / sizeof(int));

// test for insert sort
memcpy(pdarr, darr, sizeof(darr));
insert_sort(pdarr, sizeof(darr) / sizeof(double));
std::cout << "after insort sort:\n";
display(std::cout, pdarr, sizeof(darr) / sizeof(int));

delete[] piarr;
delete[] pdarr;
}

Best regards,

Tom
 
B

Bernd Strieder

Hello,

Wayne said:
template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f = std::less<T>())
What's wrong??
Why the default arguement doesn't work??

What you basically intended is a default template parameter. (You even
wanted it to be deduced through a default argument to a function
parameter.)

template <typename T, typename FUN = std::less<T>>
void insert_sort(T *arr, size_t size, FUN f = FUN())

But this is clearly illegal, my compiler says:

error: default template arguments may not be used in function templates

This is state of the art in the current standard, and according to the
book "C++ Templates" by Vandevoorde and Josuttis, it is being worked
on. Currently, the template parameters of a function template must be
deducible from the call expression.

You can overload as T. Tutone suggested. The nice point is that this can
be done uniformly with a few lines. If the standard gets extended to
allow default parameters for function templates those trivial overloads
may disappear again. I don't know whether this case can be really
considered, because it could become hairy to mix template argument
deduction, and function overload resolution that much.

template <typename T, typename FUN>
void insert_sort(T *arr, size_t size, FUN f)
{...
}

template <typename T>
void insert_sort(T *arr, size_t size)
{
insert_sort(arr, size, std::less<T>);
}

Bernd Strieder
 
W

Wayne Shu

thanks, I forgot that we can the trick of overload function
"Bernd Strieder дµÀ£º
"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top