On 12.04.2020 17:20, Juha Nieminen wrote:
> Ben Bacarisse <
ben.u...@bsb.me.uk> wrote:
>>> The problem with this is that this effetively "removes const" from
>>> the supplied types. Or, rather, Params_t expands to the non-const
>>> versions of the input types. So, for example, calling it with
>>> something like:
>>>
>>> foo("hello");
>>>
>>> will make Params_t be a "char*", not a "const char*".
>>
>> Won't it make it char [6]? When taking a reference, "hello" has type
>> const char [6].
>
> You may be right. However, the problem I'm referring to also exhibits
> itself with things like:
>
> const char* str = "hello";
> foo(str);
>
> Params_t will expand to char*, not const char*, so it becomes problematic
> if I would want to use Params_t inside the function.
Since it's a reference it won't remove the pointee's `const`.
It does remove the item `const`-ness for an array type, because a const
raw array of T is a raw array of const T.
---------------------------------------------------------------
#include <iostream>
#include <typeinfo>
using namespace std;
template< class... Params_t >
void foo( const Params_t&... )
{
((cout << typeid( Params_t ).name() << endl), ...);
}
auto main()
-> int
{
const char* s = "Hello";
const auto& t = "Hello";
foo( s, t );
}
---------------------------------------------------------------
Result with Visual C++ 2019:
char const * __ptr64
char [6]
> The "universal reference" syntax kind of fixes *that* particular problem,
> but introduces others (in a way, it's "too good" for this purpose).
> If I really wanted to take all parameters by const reference, not by
> universal reference, it seems difficult.
In short, there is a problem, but not the one you think.
- Alf