By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,601 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,601 IT Pros & Developers. It's quick & easy.

Why this code can't be compiled??

P: n/a
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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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

Nov 9 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a

Wayne Shu wrote:
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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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));
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:

insert_sort<double, std::less<double(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??
Because the compiler can't deduce the type of FUN using the default
argument when it depends upon the type of another template parameter.
Thanks.
Best regards,

Tom

Nov 9 '06 #2

P: n/a
Wayne Shu wrote:
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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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?
Nov 9 '06 #3

P: n/a
VJ
Thomas Tutone wrote:
>>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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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?
Nov 9 '06 #4

P: n/a
"VJ" <v@asd.orgwrote in message
news:ei**********@el-srv04-CHE.srvnet.eastlink.de...
Thomas Tutone wrote:
>>>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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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?
Nov 9 '06 #5

P: n/a

"Thomas Tutone д
"
Wayne Shu wrote:
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[i], key) ){
arr[i+1] = arr[i];
i = i - 1;
}
arr[i+1] = key;
}
}

template <typename T>
void display(std::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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));
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:

insert_sort<double, std::less<double(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??

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??
Thanks.
Best regards,

Tom
Nov 10 '06 #6

P: n/a
Wayne Shu wrote:
"Thomas Tutone д
Wayne Shu wrote:
Why this code can't be compiled??
Because it's not legal C++.
[snip]
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??
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[i], key) ) {
arr[i+1] = arr[i];
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::ostream &out, const T *arr, size_t size)
{
out << "<";
for(size_t i = 0; i < size; ++i){
out << arr[i] << ((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

Nov 10 '06 #7

P: n/a
Hello,

Wayne Shu wrote:
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

Nov 10 '06 #8

P: n/a
thanks, I forgot that we can the trick of overload function
"Bernd Strieder д
"
Hello,

Wayne Shu wrote:
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
Nov 11 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.