是在定义的const值参数,但不是声明真的C ++? [英] Is the const value parameter in definition but not declaration really C++?

查看:99
本文介绍了是在定义的const值参数,但不是声明真的C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于(但不同于)这个问题



这里是一些简单的测试代码来说明我发现与Sun CC的奇怪:

  // --------------- main.cpp 
#includewtc.hpp

int main(int,char **)
{
testy t;
t.lame(99);
return 0;
}
//-------------wtc.hpp
#ifndef WTC_HPP_INCLUDED
#define WTC_HPP_INCLUDED

class testy
{
public:
void lame(int);
};

#endif

//---------------wtc.cpp
#include< iostream>
#includewtc.hpp

void testy :: lame(const int a)
{
std :: cout< 我被通过< a<< \\\
;
}

// --------------- makefile
#CXX = CC
CXX = g ++
#CXXFLAGS = -g
CXXFLAGS = -g3 -Wall -Werror

OBJECTS = $(patsubst%.cpp,%。o,$(wildcard * .cpp))

all:$(OBJECTS)
$(CXX)$(CXXFLAGS)-o $ @ $ ^

.PHONY:clean
clean:
rm * .o

当使用g ++编译时,它编译,链接,跑。
你也可以添加一个++ a;在testy :: lame()和编译器将抱怨更改只读变量(因为它应该)。



但是当我编译使用CC,以下链接程序错误:

  CC -g -c -o main.o main.cpp 
CC -g -c -o wtc.o wtc.cpp
CC -g -o all main.o wtc.o
未定义首先引用
文件中的符号
void testy :: lame(int) main.o
ld:fatal:引用符号引用错误。没有输出写入所有
make:*** [all]错误1

目标代码与nm和C ++ filt,我发现g ++版本创建一个
testy :: lame(int)符号,而CC创建testy :: lame(const int),因此链接器错误。 p>

我在Stroustrup的书里看了一下,但是找不到这个技术(不是说它不存在!所以这是真的一个编译器错误,或者只是一个黑客,其他地方工作,但Solaris?

解决方案

这看起来像是 CC 中的编译器问题。 C ++标准说明(在13.1可重载声明中):


只有const和/或volatile存在或不存在的参数声明当量。也就是说,当确定正在声明,定义或调用哪个函数时,将忽略每个参数类型的const和volatile类型说明符。


但是有 const / volatile 修饰符可以参与重载,如标准稍后提到的: p>


只有在参数类型规范最外层的const和volatile类型说明符以这种方式被忽略;隐藏在参数类型规范中的const和volatile类型说明符是重要的,可用于区分重载函数声明。



This is similar to (but different from) this question.

Here is some simple test code to illustrate some weirdness I have discovered with Sun CC:

//---------------main.cpp
#include "wtc.hpp"

int main(int, char**)
{
  testy t;
  t.lame(99);
  return 0;
}
//--------------wtc.hpp
#ifndef WTC_HPP_INCLUDED
#define WTC_HPP_INCLUDED

class testy
{
public:
  void lame(int );
};

#endif 

//---------------wtc.cpp
#include <iostream>
#include "wtc.hpp"

void testy::lame(const int a)
{
  std::cout << "I was passed " << a << "\n";
}

//---------------makefile
#CXX=CC
CXX =g++
#CXXFLAGS= -g 
CXXFLAGS= -g3 -Wall -Werror

OBJECTS=$(patsubst %.cpp,%.o,$(wildcard *.cpp))

all : $(OBJECTS)
    $(CXX) $(CXXFLAGS) -o $@ $^

.PHONY: clean
clean :
    rm *.o

When this was compiled using g++ it compiles, links and does what you would expect when run. You can also add a ++a; in testy::lame() and the compiler will complain about changing a read-only variable (as it should).

However when I compile using CC, I get the following linker error:

CC -g   -c -o main.o main.cpp
CC -g   -c -o wtc.o wtc.cpp
CC -g -o all main.o wtc.o
Undefined                       first referenced
 symbol                             in file
void testy::lame(int)               main.o
ld: fatal: Symbol referencing errors. No output written to all
make: *** [all] Error 1

checking the object code with nm and C++filt, I find that the g++ version creates a testy::lame(int) symbol, whereas CC creates testy::lame(const int) , hence the linker error.

I looked it up in Stroustrup's book, but can't find this technique mentioned (doesn't mean it's not there!); so is this really a compiler bug, or just a hack that works everywhere else but Solaris?

解决方案

This looks like a compiler problem in CC. The C++ standard says (in 13.1 Overloadable declarations):

Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called.

But there are const/volatile modifiers that can participate in overloading, as the standard mentions shortly afterwards:

Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations.

这篇关于是在定义的const值参数,但不是声明真的C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆