473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why this code can't be compiled??

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
8 1529

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
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
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
"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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
13
by: AFKAFB | last post by:
Hi Sometimes when i edit previously saved VBA code e.g. To update field names etc the revised code does not work. Even if i copy and paste in a previous version it does not work. The only...
3
by: Göran Carlefyr | last post by:
Hi everyone, i've heard that aspx-files can be compiled code so that the webserver just have to deal with the code once, is that true? I'm a complete newbie to .NET, i've never made anything in...
37
by: Alan Silver | last post by:
Hello, Newbie here, so please forgive what is probably a basic question ... I see a lot of discussion about "code behind", which if I have understood correctly, means that the script code goes...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
21
by: Sandy | last post by:
Hello - I am using Visual Studio .Net. I need an example of how to construct a class that can be used throughout a project where I can include its subs and functions in various pages in the...
2
by: Bit byte | last post by:
I have some IP (intellectual property) concerns regarding IL code. 1). If I compile a native (i.e. 'unmanaged') C++ project (i.e. application or library) using C++ and the VC7.1 compiler tools,...
2
by: CodeMonkey775 | last post by:
I'm having problems passing a variable to a method which is executed and compiled using CodeDom. The situation is I have a List<CellData> with cells, each containing a formula (like Excel). I am...
15
by: colemanj4 | last post by:
Here is what I have so far, it loops while the PW is incorrect, or until cancel is selected. I want it to lock the tables for adds, deletes, and edits when cancel is selected, and if the PW is...
8
by: Max | last post by:
Is it possible for an ASP.net server (like IIS 6) to run web application without having available the code behind forms in clear ascii? Can I provide a compiled product to the customer, not an exe...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.