Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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++_Templates - Fatal编程技术网

C++ 模板相关参数类型

C++ 模板相关参数类型,c++,templates,C++,Templates,我不明白为什么这不起作用: template <typename T> struct TypeWrapper { typedef T type; }; template <> struct TypeWrapper<char*> { typedef std::string type; }; template <> struct TypeWrapper<const char*> { typedef std::stri

我不明白为什么这不起作用:

template <typename T>
struct TypeWrapper
{
    typedef T type;
};
template <>
struct TypeWrapper<char*>
{
    typedef std::string type;
};
template <>
struct TypeWrapper<const char*>
{
    typedef std::string type;
};
template <int N>
struct TypeWrapper<char[N]>
{
    typedef std::string type;
};
template <int N>
struct TypeWrapper<const char[N]>
{
    typedef std::string type;
};

class A
{
public:
    template< typename T > 
    A( const typename TypeWrapper<T>::type& t )
    {
        // do smthing
        std::cout << t << std::endl;
    }
};

int main( void )
{
    A a( 42 );

    return 0;
}
如果我将A的构造函数更改为此构造函数,它将工作:

A( const T& t )

但是我想将char*类型处理为std::strings,可能还有其他类型的调整,而不需要复制构造函数(定义一个特定于每种类型的构造函数,这是可行的)

我认为下面的语法是不正确的

A( typename const TypeWrapper<T>::type& t )

不能像这样推断模板参数。这与当你使用一个
std::vector
时,尝试推断是一样的;实际上,我很自然地输入了const类型名。。。第一,你在问题中看到的是我实验的结果:)至于你答案的最后一部分,我不确定我是否明白…@foke我不确定你所说的3种排列。你不清楚哪一部分?我想避免重载,因为在现实世界中,构造函数没有那么琐碎,我指的是常数的置换,最后一部分我指的是你解释为什么它永远不会work@foke那么也许这个样本(这是正确的)不够钝(尽管我认为是):您没有专门化函数;你让他们超负荷工作。你专门研究类型;不是功能。(成员或其他)。你所要求的将相当于
std::pair v(42,“foo”)
不知何故知道这一点,因为
42
是一个
int
“foo”
是一个
const char*
魔法,随后出现在
std::pair
模板类型推断声明上,这根本不是语言的工作方式。
A( typename const TypeWrapper<T>::type& t )
A( const typename TypeWrapper<T>::type& t )
A( typename TypeWrapper<T>::type const& t )
class A
{
public:
    template< typename T > 
    A( T const& t )
    {
        // do smthing
        std::cout << t << std::endl;
    }

    A( std::string const& s )
    {
       std::cout << "string" << std::endl;
    }

    A ( char const *s )
    {
       std::cout << "char *" << std::endl;
    }

    template<std::size_t N>
    A ( const char (&arr)[N] )
    {
       std::cout << "char array" << std::endl;
    }
};