Hello,

I'm not sure if this is the right place to post this question, but I am struggling with templates in C++.

What I would like to happen, where new List(length, arr) creates a new List based on what it is assigned to:

Code:

List<int> list = new List(0, {});
List<double> double_list = new List(1, {2});
List<char> char_list = new List(1, {'a');


Unfortunately, this doesn't work as "template argument deduction/substitution failed".
According to the internet, C++ cannot detect templates based on assignment, but I feel like this should be possible somehow.

To be clear, I want to be able to do this:

Code:

List<T> list = new List(length, {some Ts});

Such that creating the list does not specify T, but it is obvious because of the assignment.

Does anyone have any suggestions of how I should go about doing this?
A few problems here.
  1. It's telling you that you need to include the T on the constructor, a la List<T> list = new List<T>(length, {some Ts});
  2. You're trying to assign a pointer to a non-pointer variable. Either List<T>* list = new List<T>(length, {some Ts}); or List<T> list(length, {some Ts});
  3. If you do intend to use pointers, don't make them bare pointers; use shared_ptr or unique_ptr with the appropriate make_. That is, e.g., std::shared_ptr<List<T>> = std::make_shared<List<T>>(length, {some Ts})
Thank you!

I didn't realise that new List(...) was going to create a pointer, not an instance, so replacing it with List<T> list(...); fixed my problem.
Great, I'm glad that did the trick. Feel free to keep this thread as "dr-carlos's C++ Questions" if that would be useful.
Yes, that probably will be useful Smile

On the topic of templates, is there a way to create a List<T> using assignment?
Declaring it with List<T> list(...); works, but it would be more convenient for me if I could do List<T> list = something;.

I'm not sure it's possible, but wanted to check.
Yep, you can do exactly that with the copy construction!

Code:
List<float> list1(1, {3.14});
List<float> list2 = list1;

You can also build a constructor that takes an initializer list if you're making your own class, so that you could do List<T> mylist = {foo, bar, baz...};.
KermMartian wrote:
Yep, you can do exactly that with the copy construction!

Code:
List<float> list1(1, {3.14});
List<float> list2 = list1;

You can also build a constructor that takes an initializer list if you're making your own class, so that you could do List<T> mylist = {foo, bar, baz...};.

Cool. I have my own List<T> class, and have done some research on the intializer list, but cannot figure out how I would go about creating a function which allows this assignment to occur.
I have tried overloading the assignment operator:

Code:

List<T> &operator=(const T item[]) { return List<T>(1, item); }

But this doesn't seem to work, raising the following error:

Code:

error: could not convert '{String(((const char*)"hi"))}' from '<brace-enclosed initializer list>' to 'List<String>'
   12 |         List<String> str_list = {String("hi")};
      |                                              ^
      |                                              |
      |                                              <brace-enclosed initializer list>
The compiler won't unpack that single item out of the braces. You're looking for:

Code:
List<String> str_list = String("hi");


Edit: incidentally, your operator is also returning a List<T>, not a reference to one.
KermMartian wrote:
The compiler won't unpack that single item out of the braces. You're looking for:

Code:
List<String> str_list = String("hi");


Edit: incidentally, your operator is also returning a List<T>, not a reference to one.


Sorry if I just don't understand what is happening, but I have tried using

Code:
List<String> str_list = String("hi");


This raises a similar error:

Code:
error: conversion from 'String' to non-scalar type 'List<String>' requested
   12 |         List<String> str_list = String("hi");
      |                                 ^~~~~~~~~~~~


I have tried to add another operator to handle this but it still doesnt work. Also, is it a problem not returning a reference? That is why I am trying to implement a custom operator. Here are the two operators I am using:

Code:
List<T> &operator=(const T item) { return List<T>(1, {item}); }
List<T> &operator=(const T item[]) { return List<T>(1, item); }
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement