Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » prototype not found (MinGW doesnt understand defaults)
prototype not found [message #51851] Sun, 09 June 2019 18:59 Go to next message
Leander is currently offline  Leander
Messages: 9
Registered: January 2019
Promising Member
Hi together.

Have got this code here:
#include <string>
#include <stdlib.h>	
using namespace std;

template <typename T>
T* new_(int size=1, bool init=false);

template <typename T>
T* new_(int size, bool init)
{
	if (size < 1) {return NULL;}
	T *p = NULL;
	if (size==1) {p = new T;}
	else {p = new T[size];}
	if (!p) exit(-1);
	if (init) memset(p, 0, sizeof(T) * size);
	return p;
}

int main()
{
	std::string *pst;
	pst = (string*)new_();
	return 0;
}


The return "pst" should be enough to know what is a T.
But even the cast doesnt help (couldn't deduce...), see below.
Now I can call it with args or without and the MinGW will rant it (return.txt):

main.cpp: In function 'int main()':
main.cpp:28:22: error: no matching function for call to 'new_()'
main.cpp:28:22: note: candidate is:
main.cpp:14:4: note: template<class T> T* new_(int, bool)
main.cpp:14:4: note:   template argument deduction/substitution failed:
main.cpp:28:22: note:   couldn't deduce template parameter 'T'


And it suggests the only existing always, the upper prototype,
but doesnt accept it.
Someone has got a tip?

Martin
Re: prototype not found [message #51859 is a reply to message #51851] Mon, 10 June 2019 16:55 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
Type conversion operator will be able to deduct type in your case.
struct S {
  template <typename T>
  operator T() const;
};


Regards,
Novo
Re: prototype not found [message #51882 is a reply to message #51859] Tue, 11 June 2019 18:45 Go to previous messageGo to next message
Leander is currently offline  Leander
Messages: 9
Registered: January 2019
Promising Member
Hi Novo

is running, have declared now
S<std::string> s;

to get a string*, accepts the defaults now also.

Thank you
Leander
Re: prototype not found [message #51887 is a reply to message #51882] Tue, 11 June 2019 20:17 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
The code is supposed to look like below Rolling Eyes
#include <Core/Core.h>

using namespace Upp;

struct new_ {
	new_(int size=1, bool init=false)
	: size(size)
	, init(init)
	{}
	
	template <typename T>
	operator T() const { NEVER(); return T(); }
	
	template <typename T>
	operator T*() const {
		using T0 = typename std::remove_const<T>::type;
		if (size < 1) {return NULL;}
		T0 *p = NULL;
		p = new T0[size];
		if (!p) exit(-1);
		if (init) memset(p, 0, sizeof(T) * size);
		return p;
	}
	
private:
	const int size;
	const bool init;
};

CONSOLE_APP_MAIN
{
	const char* str1 = new_(12, true);
	std::string* pst = new_(12);
	delete [] pst;
	delete [] str1;
}


Regards,
Novo
Re: prototype not found [message #51888 is a reply to message #51887] Tue, 11 June 2019 20:21 Go to previous messageGo to next message
Novo is currently offline  Novo
Messages: 1358
Registered: December 2006
Ultimate Contributor
You should even be able to write code like this:
	new_ new12(12);
	const char* str2 = new12;
	std::string* pst2 = new12;


Regards,
Novo
Re: prototype not found [message #51903 is a reply to message #51888] Thu, 13 June 2019 09:49 Go to previous messageGo to next message
Leander is currently offline  Leander
Messages: 9
Registered: January 2019
Promising Member
Not bad. Going to try this soon.
Thanks for your effort.

Martin
Re: prototype not found [message #51930 is a reply to message #51851] Fri, 21 June 2019 17:59 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Leander wrote on Sun, 09 June 2019 18:59
Hi together.

Have got this code here:
#include <string>
#include <stdlib.h>	
using namespace std;

template <typename T>
T* new_(int size=1, bool init=false);

template <typename T>
T* new_(int size, bool init)
{
	if (size < 1) {return NULL;}
	T *p = NULL;
	if (size==1) {p = new T;}
	else {p = new T[size];}
	if (!p) exit(-1);
	if (init) memset(p, 0, sizeof(T) * size);
	return p;
}

int main()
{
	std::string *pst;
	pst = (string*)new_();
	return 0;
}


The return "pst" should be enough to know what is a T.


No, it is not. Types only can get resolved as parameters. But, good news, you can specify it directly:

pst = new_<string>();

[Updated on: Fri, 21 June 2019 17:59]

Report message to a moderator

Re: prototype not found [message #51954 is a reply to message #51930] Mon, 24 June 2019 21:46 Go to previous message
Leander is currently offline  Leander
Messages: 9
Registered: January 2019
Promising Member
Hi Mirek

Ooops, never considered to write it like so, and it is working.
Thanks so much for the hint.

Leander
Previous Topic: What is the difference between the memory management in C and C++?
Next Topic: Nested template question
Goto Forum:
  


Current Time: Thu Mar 28 15:31:59 CET 2024

Total time taken to generate the page: 0.01173 seconds