473,386 Members | 1,706 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,386 software developers and data experts.

Can temporary array as argument.

Hi,

It seems that an temporary can not be an argument. Since the last two
statements below does not work. I have to have declare one array and
then initialized an A object with the array, which is cumbersome. I'm
wondering if there is any walkaround, or I have to until the new C++
standard?

Thanks,
Peng

#include <vector>
#include <iostream>

template <typename T>
class A {
public:
template <size_t N>
A(const T(&v)[N]) {
for(size_t i = 0; i < N; ++ i)
_v.push_back(v[i]);
}
private:
std::vector<T_v;
};

int main() {
double v[] = {0, 1, 2, 3 };
A<doublea1(v);
A<doublea2 = v;
A<doublea3 = {0, 1, 2, 3};//error
A<doublea4({0, 1, 2, 3});//error
}
Sep 11 '08 #1
1 2020
On Sep 11, 4:48 pm, Peng Yu <PengYu...@gmail.comwrote:
Hi,

It seems that an temporary can not be an argument. Since the last two
statements below does not work. I have to have declare one array and
then initialized an A object with the array, which is cumbersome. I'm
wondering if there is any walkaround, or I have to until the new C++
standard?

Thanks,
Peng

#include <vector>
#include <iostream>

template <typename T>
class A {
public:
template <size_t N>
A(const T(&v)[N]) {
for(size_t i = 0; i < N; ++ i)
_v.push_back(v[i]);
}
private:
std::vector<T_v;

};

int main() {
double v[] = {0, 1, 2, 3 };
A<doublea1(v);
A<doublea2 = v;
A<doublea3 = {0, 1, 2, 3};//error
A<doublea4({0, 1, 2, 3});//error

}
I thought of a workaround, but requires enormous amounts of repetitive
code.

//Basically it would look like this in use:

A<doublea( bx(0.0, 1.0, 2.0, 3.0).a );

//And it would be implemented like this:

template <class, intstruct ArrayWrapper;

template <class T>
struct ArrayWrapper<T, 1>
{
T a[1];
ArrayWrapper( T t0 )
{
a[0] = t0;
}
};

template <class T>
struct ArrayWrapper<T, 2>
{
T a[2];
ArrayWrapper( T t0, T t1 )
{
a[0] = t0;
a[1] = t1;
}
};

// etc. up to ArrayWrapper<T, 10maybe

template <class T>
ArrayWrapper< T, 1 bx( T t0 )
{
return ArrayWrapper< T, 1 >( t0 );
}

template <class T>
ArrayWrapper< T, 2 bx( T t0, T t1 )
{
return ArrayWrapper< T, 2 >( t0, t1 );
}

//etc.

I can't imagine the problem is so cumbersome as to warrant this
monstrosity, but I thought I'd throw it out there for you.
Sep 12 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Kai-Uwe Bux | last post by:
Hi folks, I have trouble writing a class, derving from stringstream, that collects item and once it's done will write them to std::cout in one go. It works fine except when I use it as a...
5
by: White Wolf | last post by:
Hi, I would like to double check how long a temporary returned by a function lives? Suppose I have an instance of a class type C, which has a member function returning some sort of...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
3
by: AFN | last post by:
I need to manually create the data to be shown in a datagrid (or some data table object). Should I create an array and bind the array to the datagrid OR should I create a temporary dataset and...
2
by: xllx.relient.xllx | last post by:
Assuming compiler optimizations are set to off, specifically to not allow the compiler to elide the copy constructor, would the following hold true?: If you call a function with an user-defined...
1
by: martinezfive | last post by:
Hi, According to ISO C++ (7.11/2), "An object declared without a storage-class-specifier at block scope or declared as a function parameter has automatic storage duration by default." What about...
17
by: Klaas Vantournhout | last post by:
Hi all, I was wondering if it is possible if you can check in a function if one of the arguments is temporary. What I mean is the following. A is a class, foo is a function returning a class...
2
by: anon.asdf | last post by:
Hello! 1) =============================== When trying to define an array of std::string ... func( (std::string ) { std::string("ab"), std::string("cd"), std::string("ef") } , 3 ); ...
5
by: Juha Nieminen | last post by:
Let's assume we have a class like this: //--------------------------------------------------------- #include <iostream> class MyClass { public: MyClass() { std::cout << "constructor\n"; }...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.