Skip to content

Commit

Permalink
常见错误:cast loses precision
Browse files Browse the repository at this point in the history
  • Loading branch information
xingmingjie committed Jun 27, 2014
1 parent 6f888fa commit 11fe7e4
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 3 deletions.
451 changes: 451 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
100-gcc-tips
============
# 《100个gcc小技巧》

一个关于gcc使用小技巧的文档。100,在这里可能只是表明很多;具体的数目取决于您的参与和贡献。

## 在线阅读
[开始阅读](<https://github.com/hellogcc/100-gcc-tips/blob/master/src/index.md>)

## 如何参与

直接发PULL REQUEST,或与我们联系。

增加一个小技巧的步骤:

1. 在src目录下新增一个md文件,参照现有文件的格式风格,编写一个小技巧
markdown语法参见 http://wowubuntu.com/markdown/
md文件编写可以使用在线所见即所得编辑器 https://www.zybuluo.com/mdeditor
2. 在index.md中为新md文件增加一个索引,可以放到已有分类中,或增加一个分类
3. 如果预览下没有问题,OK!

本地生成html的步骤:

1. 确保[go](http://code.google.com/p/go)[md2min](https://github.com/fairlyblank/md2min)已经安装并可用
2. 直接运行build.sh
3. 如果顺利,会在html目录下生成所有的html文件

## 联系方式

- [博客网站](http://www.hellogcc.org)
- 在线讨论问题:IRC, freenode, #hellogcc房间
- [邮件列表](http://www.freelists.org/list/hellogcc) (发信需要先订阅)

## 版权

本文档版权归贡献者所有。

## 授权许可

本文档使用的是[GNU Free Documentation License](http://www.gnu.org/licenses/fdl.html)

## 致谢

- 各位参与者

## 其它资源

- [GCC在线手册](https://gcc.gnu.org/onlinedocs/gcc)

A collection of gcc tips. 100 maybe just mean many here.
27 changes: 27 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh

# This script invokes md2min to convert markdown files to minimal html files,
# using github css. So go and md2min should be available before you run it.
# See https://github.com/fairlyblank/md2min
# See http://code.google.com/p/go

TOPDIR=`dirname $0`

if [ ! type -P go >/dev/null 2>&1 ]; then
echo "error: can't find go, which is necessary for building html"
exit 0
fi

if [ ! type -P md2min >/dev/null 2>&1 ]; then
echo "error: can't find md2min, which is necessary for building html"
exit 0
fi

mkdir -p "$TOPDIR/html"

(
export SRC="$TOPDIR/src"
export HTML="$TOPDIR/html"
go run "$TOPDIR/utils/build.go"
)

47 changes: 47 additions & 0 deletions src/cast-lose-precision.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# error: cast from ... to ... loses precision

## 例子

#include <iostream>

class Foo {
public:
void print() const {
std::cout << (int)(this) << "\n";
}
};

int main()
{
class Foo foo;

foo.print();
return 0;
}

## 技巧

在g++编译上面的例子,会报如下错误:

$ g++ foo.cc
foo.cc: In member function ‘void Foo::print() const’:
foo.cc:6:28: error: cast from ‘const Foo*’ to ‘int’ loses precision [-fpermissive]

这是一个强制类型转换的错误,你可以修改源代码为:

std::cout << (int*)(this) << "\n";

即可。

如果,你不想(或不能)去修改源程序,只是应为升级了gcc而带来了这样的错误,那么也可以使用`-fpermissive`选项,将错误降低为警告:

$ g++ foo.cc -fpermissive
foo.cc: In member function ‘void Foo::print() const’:
foo.cc:6:28: warning: cast from ‘const Foo*’ to ‘int’ loses precision [-fpermissive]

详情参见[gcc手册](https://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Dialect-Options.html#index-fpermissive-166)

## 贡献者

xmj

3 changes: 3 additions & 0 deletions src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# 常见错误
* [error: cast from ... to ... loses precision](cast-lose-precision.md)

111 changes: 111 additions & 0 deletions utils/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 代码源自https://github.com/astaxie/build-web-application-with-golang.git

package main

import (
"fmt"
"github.com/fairlyblank/md2min"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)

// 定义一个访问者结构体
type Visitor struct{}

func (self *Visitor) md2html(arg map[string]string) error {
from := arg["from"]
to := arg["to"]
err := filepath.Walk(from+"/", func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
if (f.Mode() & os.ModeSymlink) > 0 {
return nil
}
if !strings.HasSuffix(f.Name(), ".md") {
return nil
}

file, err := os.Open(path)
if err != nil {
return err
}

input_byte, _ := ioutil.ReadAll(file)
input := string(input_byte)
input = regexp.MustCompile(`\[(.*?)\]\(<?(.*?)\.md>?\)`).ReplaceAllString(input, "[$1](<$2.html>)")

if f.Name() == "README.md" {
input = regexp.MustCompile(`https:\/\/github\.com\/astaxie\/build-web-application-with-golang\/blob\/master\/`).ReplaceAllString(input, "")
}

// 以#开头的行,在#后增加空格
// 以#开头的行, 删除多余的空格
input = FixHeader(input)

// 删除页面链接
input = RemoveFooterLink(input)

var out *os.File
filename := strings.Replace(f.Name(), ".md", ".html", -1)
fmt.Println(to + "/" + filename)
if out, err = os.Create(to + "/" + filename); err != nil {
fmt.Fprintf(os.Stderr, "Error creating %s: %v", f.Name(), err)
os.Exit(-1)
}
defer out.Close()
md := md2min.New("none")
err = md.Parse([]byte(input), out)
if err != nil {
fmt.Fprintln(os.Stderr, "Parsing Error", err)
os.Exit(-1)
}

return nil
})
return err
}

func FixHeader(input string) string {
re_header := regexp.MustCompile(`(?m)^#.+$`)
re_sub := regexp.MustCompile(`^(#+)\s*(.+)$`)
fixer := func(header string) string {
s := re_sub.FindStringSubmatch(header)
return s[1] + " " + s[2]
}
return re_header.ReplaceAllStringFunc(input, fixer)
}

func RemoveFooterLink(input string) string {
re_footer := regexp.MustCompile(`(?m)^#{2,} links.*?\n(.+\n)*`)
return re_footer.ReplaceAllString(input, "")
}

func main() {
html := os.Getenv("HTML")
if html == "" {
html = "."
}

src := os.Getenv("WORKDIR")
if src == "" {
src = "."
}

arg := map[string]string{
"from": src,
"to": html,
}

v := &Visitor{}
err := v.md2html(arg)
if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
}
}

0 comments on commit 11fe7e4

Please sign in to comment.