Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;模板类型演绎:如何将T转换为常量T_C++_Templates_C++14 - Fatal编程技术网

C++ c++;模板类型演绎:如何将T转换为常量T

C++ c++;模板类型演绎:如何将T转换为常量T,c++,templates,c++14,C++,Templates,C++14,我需要根据结构成员的类型使用宏定义函数 例如: 我需要定义以下函数- uint32_t fun () {...} const uint8_t *fun () {...} << note the pointer types needs a const uint32\u t fun(){…} const uint8_t*fun(){…} const T_udecayFunction(const T)//使其成为const T { 返回t; } 模板< 类型名T, typename s

我需要根据结构成员的类型使用宏定义函数

例如:

我需要定义以下函数-

uint32_t fun () {...}
const uint8_t *fun () {...}  << note the pointer types needs a const
uint32\u t fun(){…}
const uint8_t*fun(){…}
const T_udecayFunction(const T)//使其成为const T
{
返回t;
}
模板<
类型名T,
typename std::如果\u t::value>*=nullptr,则启用\u
>
decltype(自动)\衰减功能(T)
{
返回t;
}
模板
结构返回类型{
私人:
使用U=typename std::remove_reference::type;
公众:
使用type=decltype(uu decayFunction(std::declval());
};
return_type::type fun(){…}
但是我看到上面函数的返回类型不是const

如何使其成为常量?

以下工作-

template <typename T>
const T *as_const_ptr (const T *p)
{
    return p;
}

template <
             typename T,
             typename std::enable_if_t<std::is_pointer<T>::value>* = nullptr
         >
auto __decayFunction (T t) -> decltype(as_const_ptr(t)) { return as_const_ptr(t); }

template <
             typename T,
             typename std::enable_if_t<!std::is_pointer<T>::value>* = nullptr
         >
decltype(auto) __decayFunction (T t)  { return t; }

template<class T>
struct return_type {
private:
        using U = typename std::remove_reference<T>::type;
public:
        using type = decltype(__decayFunction(std::declval<U>()));
};
模板
常数T*as_const_ptr(常数T*p)
{
返回p;
}
模板<
类型名T,
typename std::如果\u t*=nullptr,则启用\u
>
自动uuu decayFunction(T)->decltype(as_uconst_ptr(T)){返回as_const_ptr(T);}
模板<
类型名T,
typename std::如果\u t::value>*=nullptr,则启用\u
>
decltype(auto)uu decayFunction(T){return T;}
模板
结构返回类型{
私人:
使用U=typename std::remove_reference::type;
公众:
使用type=decltype(uu decayFunction(std::declval());
};

C++中不允许使用零大小数组,我知道,但它是一个GCC和CLANG扩展,我必须使用它-因为它是遗留代码的一部分。它不是CLANG(或CLANG)。我想它的GCC和GCCI不确定。因此我没有接受答案:-)我正在等待模板专家的解释。当作为函数参数传递时,T被推断为指针类型。所以w/chara[10],T将是char*,以某种方式将常量添加到被忽略。在函数签名为T*
template const T*\u decayFunction(const T*T){return T;}
template<class T>
struct decay_zero { using type = std::decay_t<T>; };

template<class T>
struct decay_zero<T[]> { using type = const T *; };

template<class T, size_t N>
struct decay_zero<T[N]> { using type = const T *; }; // adding const to pointer type

template<class T>
struct decay_zero<T[0]> { using type = const T *; };

template<class T>
struct return_type {
private:
        using U = typename std::remove_reference<T>::type;
public:
        using type = decay_zero<U>::type;
};

return_type<decltype(A::str)>::type fun {...}
template <
             typename T,
             typename std::enable_if_t<std::is_pointer<T>::value>* = nullptr
         >
const T __decayFunction (const T t) // making it const T
{
    return return t;
}

template <
             typename T,
             typename std::enable_if_t<!std::is_pointer<T>::value>* = nullptr
         >
decltype(auto) __decayFunction (T t)
{
    return t;
}

template<class T>
struct return_type {
private:
        using U = typename std::remove_reference<T>::type;
public:
        using type = decltype(__decayFunction(std::declval<U>()));
};

return_type<decltype(A::str)>::type fun() { ... } 
template <typename T>
const T *as_const_ptr (const T *p)
{
    return p;
}

template <
             typename T,
             typename std::enable_if_t<std::is_pointer<T>::value>* = nullptr
         >
auto __decayFunction (T t) -> decltype(as_const_ptr(t)) { return as_const_ptr(t); }

template <
             typename T,
             typename std::enable_if_t<!std::is_pointer<T>::value>* = nullptr
         >
decltype(auto) __decayFunction (T t)  { return t; }

template<class T>
struct return_type {
private:
        using U = typename std::remove_reference<T>::type;
public:
        using type = decltype(__decayFunction(std::declval<U>()));
};