How can arguments to variadic functions be passed by reference in PHP?
Date : March 29 2020, 07:55 AM
help you fix your problem There is no way of passing variable length argument lists by reference in PHP. It is a fundamental limitation of the language. There is, however, a workaround with array(&$var1, &$var2...) syntax: <?php
/** raise all our arguments to the power of 2 */
function pow2() {
$args = &func_get_arg(0);
for ($i = 0; $i< count($args); ++$i) {
$args[$i] *= 2;
}
}
$x = 1; $y = 2; $z = 3;
pow2(array(&$x, &$y, &$z)); // this is the important line
echo "$x, $y, $z"; // output "2, 4, 6"
?>
|
C++0x: how to get variadic template parameters without reference?
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I don't understand your question. This code works fine with GCC and I see no reason why it shouldn't work. #include <tuple>
template<typename... Args>
void something(Args... args)
{
std::tuple<Args...> tuple{args...};
std::get<0>(tuple) = 5;
}
int main() {
int x = 10;
something<int&>(x);
}
[js@HOST2 cpp]$ g++ -std=c++0x main1.cpp
[js@HOST2 cpp]$
template<typename... Args>
void something(Args... args)
{
std::tuple<typename std::decay<Args>::type...> tuple;
std::get<0>(tuple) = 5;
}
|
C++0x variadic template pass by reference
Date : March 29 2020, 07:55 AM
wish of those help I want to use variadic template feature for my application but I don't want the objects to be passed by value (as objects are quite complex in my case). I want to pass them by reference (not as pointers). , Just say: template <typename ...Args>
int myfunction(Args & ... args)
{
/* ... */
}
|
c++ template variadic function undefined reference
Date : March 29 2020, 07:55 AM
help you fix your problem You CANNOT put template function into .cpp. you must put it into .h because compiler must know what the template is when specializing it. your first main file #include <iostream>
#include "TestTempB.h"
using namespace std;
using namespace ddl_lib;
int main() {
string aa="15615";
bool ccc=false;
TestTempB b;
b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}
#include <iostream>
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
TestTempB();
virtual ~TestTempB();
template<class ... Args>
void callCommandB(const string&, const uint32_t*, Args ...);
template<class ... Args>
void callCommandA(const string&, Args ...);
};
} /* namespace ddl_lib */
using namespace ddl_lib;
int main() {
string aa="15615";
bool ccc=false;
TestTempB b;
b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}
#include <iostream>
#include <string>
using namespace std;
namespace ddl_lib {
class TestTempB {
public:
TestTempB();
virtual ~TestTempB();
template<class ... Args>
void callCommandB(const string&, const uint32_t*, Args ...);
template<class ... Args>
void callCommandA(const string&, Args ...);
};
template<class ... Args>
void TestTempB::callCommandB(const string& cmdName, const uint32_t* cmdSn, Args ... params) {
cout << cmdName << endl;
}
template<class ... Args>
void TestTempB::callCommandA(const string& cmdName, Args ... params) {
callCommandB(cmdName, NULL, params...);
}
} /* namespace ddl_lib */
using namespace ddl_lib;
int main() {
string aa="15615";
bool ccc=false;
TestTempB b;
b.callCommandA("name", &aa, &ccc);//undefined reference to `void ddl_lib::TestTempB::callCommandA<std::string*, bool*>(std::string const&, std::string*, bool*)'
}
|
Have rvalue reference instead of forwarding reference with variadic template
Tag : cpp , By : marocchino
Date : March 29 2020, 07:55 AM
Hope that helps I have a struct X and a function foo which must receive rvalue references of X. , The way to have a bunch of rvalue references is with SFINAE: template <class... Args,
std::enable_if_t<(!std::is_lvalue_reference<Args>::value && ...), int> = 0>
void foo(Args&&... args) { ... }
template <class... Args>
requires (!std::is_lvalue_reference<Args>::value && ...)
void foo(Args&&... args) { ... }
template <class T>
concept NonReference = !std::is_lvalue_reference<T>::value;
template <NonReference... Args>
void foo(Args&&... ) { ... }
template <class... Args> auto foo(const Args&... args) = delete;
template <class... Args> auto foo(Args&... args) = delete;
|