C++模板参数更改对指针的引用

发布于 2024-10-22 06:04:53 字数 7708 浏览 1 评论 0原文

也许我不知道如何搜索,但事实上我找不到任何人谈论这个。

我的结构有一个非类型参数,该参数依赖于类型参数。

template<
    typename SpecType,
    SpecType NonType >
struct Struct
//...

SpecType 是对指针的引用(例如 const char *&)时,NonType 的行为就像它是实际的地址一样专门论证,不作为参考。 更令人惊讶的是,如果我显式地将 NonType 转换为 SpecType,一切都会按预期工作!

IBM说了一些关于转换为数组和函数指针的内容,但我不明白它与我的疑问有关。

当我创建没有嵌入模板类型(S1S2)的结构时,不会发生同样的事情。

当然我可以将其更改为:

template<
    typename SpecType,
    SpecType &NonType >

但它不会解释我所看到的。 谁能给出一个深刻的(或者愚蠢的,如果这是我的愚蠢)解释?


下面的例子有点扩展,但是看看它的输出我想我的问题会更清楚:

#include    <iostream>
#include    <typeinfo>

using namespace std;


void    f1( const char **p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const char **p ): p = \"" << p << "\"" << endl;
}

void    f1( const char *p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const char *p ): p = \"" << p << "\"" << endl;
}

void    f1( const int **p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const int **p ): p = \"" << p << "\"" << endl;
}

void    f1( const int *p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const int *p ): p = \"" << p << "\"" << endl;
}

template<
    typename SpecType,
    SpecType NonType >
struct Struct
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "SpecType is " << typeid( SpecType ).name( ) << endl;
        cout << "NonType is " << typeid( NonType ).name( ) << endl;
        cout << "NonType = \"" << NonType << "\"" << endl;
        cout << "( SpecType )NonType = \"" << ( SpecType )NonType << "\"" << endl;
        cout << "*NonType = \"" << *NonType << "\"" << endl;
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;

        f1( NonType );
    }
};

template< const char *&P >
struct  S1
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "&P = \"" << &P << "\"" << endl;
        cout << "P = \"" << P << "\"" << endl;
        cout << "*P = \"" << *P << "\"" << endl;

        f1( P );
    }
};

template< const char **P >
struct  S2
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "P = \"" << P << "\"" << endl;
        cout << "*P = \"" << *P << "\"" << endl;
        cout << "*P[ 0 ] = \"" << **P << "\"" << endl;

        f1( P );
    }
};

const char * const_pname    = "name";

const int   pint[]  = { 42, 51 };
const int   *const_pint = pint;

int main( )
{
    cout << "=============================================" << endl;
    cout << "const_pname = " << const_pname << endl;
    cout << "@const_pname = 0x" << hex << ( unsigned long )const_pname << dec << endl;
    cout << "&const_pname = 0x" << hex << ( unsigned long )&const_pname << dec << endl;

    cout << "=============================================" << endl;
    cout << "Struct< const char *&, const_pname >   constpTtname" << endl;
    Struct< const char *&, const_pname >    constpTtname;
    constpTtname.f( );

    cout << "=============================================" << endl;
    cout << "Struct< const int *&, const_pint > constpTtint" << endl;
    Struct< const int *&, const_pint >  constpTtint;
    constpTtint.f( );

    cout << "=============================================" << endl;
    cout << "S1< const_pname >  s1" << endl;
    S1< const_pname >   s1;
    s1.f( );

    cout << "=============================================" << endl;
    cout << "S2< &const_pname > s2" << endl;
    S2< &const_pname >  s2;
    s2.f( );

    return  0;
}

输出是:

$ ./nontype_mutant
=============================================
const_pname = name
@const_pname = x401624                                                                   
&const_pname = 0x601e18                                                                   
=============================================                                             
Struct< const char *&, const_pname >    constpTtname                                      
---------------------------------------------                                             
SpecType is PKc                                                                           
NonType is PKc                                                                            
NonType = "$@"                                                                            
( SpecType )NonType = "name"                                                              
*NonType = "name"                                                                         
*NonType[ 0 ] = "n"                                                                       
---------------------------------------------                                             
f1( const char *p ): p = "$@"                                                             
=============================================                                             
Struct< const int *&, const_pint >      constpTtint                                       
---------------------------------------------                                             
SpecType is PKi                                                                           
NonType is PKi                                                                            
NonType = "0x601e20"                                                                      
( SpecType )NonType = "0x4017a8"                                                          
*NonType = "0x4017a8"                                                                     
*NonType[ 0 ] = "42"                                                                      
---------------------------------------------                                             
f1( const int *p ): p = "0x601e20"                                                        
=============================================                                             
S1< const_pname >       s1
---------------------------------------------
&P = "0x601e18"
P = "name"
*P = "n"
---------------------------------------------
f1( const char *p ): p = "name"
=============================================
S2< &const_pname >      s2
---------------------------------------------
P = "0x601e18"
*P = "name"
*P[ 0 ] = "n"
---------------------------------------------
f1( const char **p ): p = "0x601e18"

Maybe I didn't know how to search, but it's fact that I couldn't find anyone talking about this.

I have struct that has a non-type argument that depends on a type argument.

template<
    typename SpecType,
    SpecType NonType >
struct Struct
//...

When SpecType is a reference to a pointer (const char *&, for example) NonType behaves as if it is the address of the actual specialized argument, and not a reference.
More surprising is that if I explicitly cast NonType to SpecType, everything works as expected!

IBM says something about conversion to pointer of arrays and functions, but I don't understand it as related to my doubt.

When I create structs that have no embedded template types (S1 and S2) the same thing does ot happen.

Of course I can change it to:

template<
    typename SpecType,
    SpecType &NonType >

but it won't explain what I see.
Can anyone please give a deep (or dumb, if it is my stupidness) explanation?


The following example is a bit extense, but looking at its output I think my problem will be more clear:

#include    <iostream>
#include    <typeinfo>

using namespace std;


void    f1( const char **p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const char **p ): p = \"" << p << "\"" << endl;
}

void    f1( const char *p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const char *p ): p = \"" << p << "\"" << endl;
}

void    f1( const int **p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const int **p ): p = \"" << p << "\"" << endl;
}

void    f1( const int *p )
{
    cout << "---------------------------------------------" << endl;
    cout << "f1( const int *p ): p = \"" << p << "\"" << endl;
}

template<
    typename SpecType,
    SpecType NonType >
struct Struct
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "SpecType is " << typeid( SpecType ).name( ) << endl;
        cout << "NonType is " << typeid( NonType ).name( ) << endl;
        cout << "NonType = \"" << NonType << "\"" << endl;
        cout << "( SpecType )NonType = \"" << ( SpecType )NonType << "\"" << endl;
        cout << "*NonType = \"" << *NonType << "\"" << endl;
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;

        f1( NonType );
    }
};

template< const char *&P >
struct  S1
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "&P = \"" << &P << "\"" << endl;
        cout << "P = \"" << P << "\"" << endl;
        cout << "*P = \"" << *P << "\"" << endl;

        f1( P );
    }
};

template< const char **P >
struct  S2
{
    void    f( )
    {
        cout << "---------------------------------------------" << endl;
        cout << "P = \"" << P << "\"" << endl;
        cout << "*P = \"" << *P << "\"" << endl;
        cout << "*P[ 0 ] = \"" << **P << "\"" << endl;

        f1( P );
    }
};

const char * const_pname    = "name";

const int   pint[]  = { 42, 51 };
const int   *const_pint = pint;

int main( )
{
    cout << "=============================================" << endl;
    cout << "const_pname = " << const_pname << endl;
    cout << "@const_pname = 0x" << hex << ( unsigned long )const_pname << dec << endl;
    cout << "&const_pname = 0x" << hex << ( unsigned long )&const_pname << dec << endl;

    cout << "=============================================" << endl;
    cout << "Struct< const char *&, const_pname >   constpTtname" << endl;
    Struct< const char *&, const_pname >    constpTtname;
    constpTtname.f( );

    cout << "=============================================" << endl;
    cout << "Struct< const int *&, const_pint > constpTtint" << endl;
    Struct< const int *&, const_pint >  constpTtint;
    constpTtint.f( );

    cout << "=============================================" << endl;
    cout << "S1< const_pname >  s1" << endl;
    S1< const_pname >   s1;
    s1.f( );

    cout << "=============================================" << endl;
    cout << "S2< &const_pname > s2" << endl;
    S2< &const_pname >  s2;
    s2.f( );

    return  0;
}

The output is:

$ ./nontype_mutant
=============================================
const_pname = name
@const_pname = x401624                                                                   
&const_pname = 0x601e18                                                                   
=============================================                                             
Struct< const char *&, const_pname >    constpTtname                                      
---------------------------------------------                                             
SpecType is PKc                                                                           
NonType is PKc                                                                            
NonType = "$@"                                                                            
( SpecType )NonType = "name"                                                              
*NonType = "name"                                                                         
*NonType[ 0 ] = "n"                                                                       
---------------------------------------------                                             
f1( const char *p ): p = "$@"                                                             
=============================================                                             
Struct< const int *&, const_pint >      constpTtint                                       
---------------------------------------------                                             
SpecType is PKi                                                                           
NonType is PKi                                                                            
NonType = "0x601e20"                                                                      
( SpecType )NonType = "0x4017a8"                                                          
*NonType = "0x4017a8"                                                                     
*NonType[ 0 ] = "42"                                                                      
---------------------------------------------                                             
f1( const int *p ): p = "0x601e20"                                                        
=============================================                                             
S1< const_pname >       s1
---------------------------------------------
&P = "0x601e18"
P = "name"
*P = "n"
---------------------------------------------
f1( const char *p ): p = "name"
=============================================
S2< &const_pname >      s2
---------------------------------------------
P = "0x601e18"
*P = "name"
*P[ 0 ] = "n"
---------------------------------------------
f1( const char **p ): p = "0x601e18"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苄①跕圉湢 2024-10-29 06:04:53

我尝试使用三个编译器来编译您的代码,其中两个编译器具有非常相似的行为,给出以下消息(大约):

test.cpp:44:41: error: indirection requires pointer operand ('int' invalid)
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;
                                        ^~~~~~~~~
test.cpp:93:18: note: in instantiation of member function 'Struct<const char *&, const_pname>::f' requested here
    constpTtname.f( );
                 ^
test.cpp:44:41: error: indirection requires pointer operand ('int' invalid)
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;
                                        ^~~~~~~~~
test.cpp:98:17: note: in instantiation of member function 'Struct<const int *&, const_pint>::f' requested here
    constpTtint.f( );
                ^
2 errors generated.

错误消息对我来说似乎是正确且不言而喻的。这是使用 clang 的结果。 Comeau 的基于 EDG 的编译器是另一个给出与此非常相似的消息的编译器。

g++ 编译了它(我认为是错误的)并给出了与您报告的类似的输出。

I've tried to compile your code using three compilers and two of them have very similar behavior giving the following message (approximately):

test.cpp:44:41: error: indirection requires pointer operand ('int' invalid)
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;
                                        ^~~~~~~~~
test.cpp:93:18: note: in instantiation of member function 'Struct<const char *&, const_pname>::f' requested here
    constpTtname.f( );
                 ^
test.cpp:44:41: error: indirection requires pointer operand ('int' invalid)
        cout << "*NonType[ 0 ] = \"" << **NonType << "\"" << endl;
                                        ^~~~~~~~~
test.cpp:98:17: note: in instantiation of member function 'Struct<const int *&, const_pint>::f' requested here
    constpTtint.f( );
                ^
2 errors generated.

The error message seems correct and self evident to me. This was the result of using clang. Comeau's EDG based compiler was the other one to give a message very similar to this.

g++ compiled it (I believe incorrectly) and gave an output similar to that you report.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文