Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why this code can't be compiled??

Reply
Thread Tools

Why this code can't be compiled??

 
 
Wayne Shu
Guest
Posts: n/a
 
      11-09-2006
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:stream &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

 
Reply With Quote
 
 
 
 
Eric
Guest
Posts: n/a
 
      11-09-2006
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:stream &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?
 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      11-09-2006

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:stream &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

 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      11-09-2006
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:stream &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?
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      11-09-2006
"VJ" <(E-Mail Removed)> wrote in message
news:eivlcn$gvn$(E-Mail Removed)...
> 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:stream &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?


 
Reply With Quote
 
Wayne Shu
Guest
Posts: n/a
 
      11-10-2006

"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:stream &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


 
Reply With Quote
 
Thomas Tutone
Guest
Posts: n/a
 
      11-10-2006
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:stream &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

 
Reply With Quote
 
Bernd Strieder
Guest
Posts: n/a
 
      11-10-2006
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

 
Reply With Quote
 
Wayne Shu
Guest
Posts: n/a
 
      11-11-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments